+ 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?

9th Feb 2018, 10:14 PM
Yusuf
Yusuf - avatar
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.
9th Feb 2018, 10:35 PM
jay
jay - avatar
+ 5
Thank you @jay
9th Feb 2018, 10:54 PM
Yusuf
Yusuf - avatar