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
*/
My question is: 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
?