Trying to understand the behaviour of pointers in C, I was a little surprised by the following (example code below):
#include <stdio.h> void add_one_v1(int *our_var_ptr) { *our_var_ptr = *our_var_ptr +1; } void add_one_v2(int *our_var_ptr) { *our_var_ptr++; } int main() { int testvar; testvar = 63; add_one_v1(&(testvar)); /* Try first version of the function */ printf("%d\n", testvar); /* Prints out 64 */ printf("@ %p\n\n", &(testvar)); testvar = 63; add_one_v2(&(testvar)); /* Try first version of the function */ printf("%d\n", testvar); /* Prints 63 ? */ printf("@ %p\n", &(testvar)); /* Address remains identical */ }
Output:
64 @ 0xbf84c6b0 63 @ 0xbf84c6b0
What exactly does the *our_var_ptr++
statement in the second function (add_one_v2
) do since it's clearly not the same as *our_var_ptr = *our_var_ptr +1
?
Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.
In C programming language, *p represents the value stored in a pointer. ++ is increment operator used in prefix and postfix expressions. * is dereference operator. Precedence of prefix ++ and * is same and both are right to left associative.
Dereference a pointer is used because of the following reasons: It can be used to access or manipulate the data stored at the memory location, which is pointed by the pointer. Any operation applied to the dereferenced pointer will directly affect the value of the variable that it points to.
In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, or l-value in memory pointed to by the variable's value.
This is one of those little gotcha's that make C and C++ so much fun. If you want to bend your brain, figure out this one:
while (*dst++ = *src++) ;
It's a string copy. The pointers keep getting incremented until a character with a value of zero is copied. Once you know why this trick works, you'll never forget how ++ works on pointers again.
P.S. You can always override the operator order with parentheses. The following will increment the value pointed at, rather than the pointer itself:
(*our_var_ptr)++;
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. }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With