j is a pointer which points to i. The first print statement returns the value of i, but when I try to deference the pointer and increment i by 1, it returns a useless value which I suppose is the address of i. Why does this happen, and where can I read about pointers in more detail?
#include <stdio.h>
int main( )
{
int i = 3, *j;
j = &i ;
printf ( "\nValue of i = %u", *j ) ;
*j ;
printf ( "\nValue of i = %u", *j ) ;
}
After *j I expect j to point to i and the value of i should now be 4. So when I print *j it should return 4.
CodePudding user response:
I think it should print 4
By that I assume you think you are incrementing i. But in fact you are incrementing the j pointer not the content that it is pointing to. This is because the C operator precedence says that has higher precedence than *.
That is, *j is doing:
j*j
The second step is a no-op as the result is not used.
What you actually want is: (*j)
CodePudding user response:
The expression
*j
is first dereferencing the pointer and then incrementing it. You want to increment the referenced value and leave the pointer untouched. This can be achieved by the following expression:
*j = 1
