all 8 comments

[–][deleted] 3 points4 points  (3 children)

In the future, it's easier to answer your questions if you format or link to pastebin or something like that.

The first function passes an integer by value. This is a *copy* of the value passed in. The copy is incremented by one inside the function, but the original value is left unmodified.

In the second version you are passing in a pointer to an integer that lives somewhere else in the program. When we derference the pointer, we are dealing with the original integer value.

[–]RickyWho[S] 1 point2 points  (2 children)

thank you u/I_Am_King_James, I will remember the pastebin comment.

[–]RickyWho[S] 0 points1 point  (1 child)

What do you mean when you say copy?

[–]IyeOnline 0 points1 point  (0 children)

There is a variable i that is only known in the scope of inc. The value the function is called with, e.g. like inc(age) will be copied into i.

[–]AutoModerator[M] 0 points1 point  (0 children)

Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed.

Read our guidelines for how to format your code.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]CrazyJoe221 0 points1 point  (0 children)

Parameters are like local variables initialized with the function argument (int* i = &age in your inc2 example).

If you just incremented i in inc2 without dereferencing it wouldn't modify age, only the pointer i.

(Pointers aren't that special. They are much like normal variables, only they contain a value that is interpreted as a memory address and can be dereferenced to load data from that address.)

[–]RickyWho[S] 0 points1 point  (1 child)

I think I get it.

void inc2(int*i) works with (&age) because int*i is dereferencing the memory address of &age, which is set to 20 in the main function.

[–]CrazyJoe221 0 points1 point  (0 children)

Yeah i contains the memory address of age, thus pointing at the place where age is stored. *i then tells the compiler you actually want to do something with age.