8 Answers
8

Due to operator precedence rules and the fact that ++ is a postfix operator, add_one_v2() does dereference the pointer, but the ++ is actually being applied to the pointer itself. However, remember that C always uses pass-by-value: add_one_v2() is incrementing its local copy of the pointer, which will have no effect whatsoever on the value stored at that address.

As a test, replace add_one_v2() with these bits of code and see how the output is affected:

void add_one_v2(int *our_var_ptr)
{
(*our_var_ptr)++; // Now stores 64
}
void add_one_v2(int *our_var_ptr)
{
*(our_var_ptr++); // Increments the pointer, but this is a local
// copy of the pointer, so it doesn't do anything.
}

That isn't true about the order of operation. In add_one_v2, the ++ is applied to the pointer, not the dereference. However, since it is a post increment, the dereference is happening BEFORE the increment.
–
TorlackMay 13 '09 at 19:12

Are you talking about the original add_one_v2, or one of my examples with parentheses?
–
htwMay 13 '09 at 19:14

I'm talking about the original. I'm just trying to point out that your statement of "is incrementing the pointer and then dereferencing..." is incorrect.
–
TorlackMay 13 '09 at 19:16

Thanks for the quick answer (and for the informative comment Torlack!). ++*our_var_ptr seems to 'work' the same as add_one_v1. One thing i don't understand though: if the functions are only working with local copies of the pointers --> how do they achieve to influence the value printed in main? Or do i misinterpret that statement?
–
ChristopheDMay 13 '09 at 19:19

2

The pointer itself is a local copy, but the value it is pointing at isn't.
–
TorlackMay 13 '09 at 19:20

Thanks for taking the time for writing this answer, it's this kind of stuff that makes StackOverflow so great an environment! I've had more insightful answers here in one hour than i would have found in the same time in my lousy C tutorials ;)
–
ChristopheDMay 13 '09 at 20:30

Thanks! I think this is the most important point - "Because ++ binds more tightly than *"
–
kumarJun 4 '14 at 17:41

Also, folks should make note of this : int q = *p++; IS NOT EQUIVALENT TO *p++; *p++ will still have "first value" only. whereas in q it is "second value".( I think you mentioned it in second point)
–
kumarJun 4 '14 at 17:43

As the others have pointed out, operator precedence cause the expression in the v2 function to be seen as *(our_var_ptr++).

However, since this is a post-increment operator, it's not quite true to say that it increments the pointer and then dereferences it. If this were true I don't think you'd be getting 63 as your output, since it would be returning the value in the next memory location. Actually, I believe the logical sequence of operations is:

Save off the current value of the pointer

Increment the pointer

Dereference the pointer value saved in step 1

As htw explained, you aren't seeing the change to the value of the pointer because it is being passed by value to the function.

our_var_ptr is a pointer to some memory. ie it is the memory cell where the data is stored. (n this case 4 bytes in the binary format of an int).

*our_var_ptr is the dereferenced pointer - it goes to the location the pointer 'points' at.

++ increments a value.

so. *our_var_ptr = *our_var_ptr+1 dereferences the pointer and adds one to the value at that location.

Now add in operator precedence - read it as (*our_var_ptr) = (*our_var_ptr)+1 and you see that the dereference happens first, so you take the value and incremement it.

In your other example, the ++ operator has lower precedence than the *, so it takes the pointer you passed in, added one to it (so it points at garbage now), and then returns. (remember values are always passed by value in C, so when the function returns the original testvar pointer remains the same, you only changed the pointer inside the function).

My advice, when using dereferencing (or anything else) use brackets to make your decision explicit. Don't try to remember the precedence rules as you'll only end up using another language one day that has them slightly different and you'll get confused. Or old and end up forgetting which has higher precedence (like I do with * and ->).

Ah! No. The ++ binds more tightly, so it is the pointer that is incremented. But it is the post-increment version, so it happens after the dereference. So: dereference the pointer and return a value, then increment the pointer (not the value pointed to).
–
dmckeeMay 13 '09 at 19:17