+ 6
A question
I saw this question in a challenge: int x=4 int *a=&x; x--; *a++; cout<<x;//The answer is 3. Why the changes made in pointer does not affect x?
2 Answers
+ 10
Hi Joseph!
Good question!
This behaviour is explained by operator precedence.
*a++ is interpreted by the compiler as *(a++)
In effect the pointer is moved, and no longer has a reference to x.
If the code was ++*a; the compiler would see ++(*a) and x would be 4.
+ 5
Thank you @jay