0

Output of C++ code

Why does this code output 4, and not 5? int x, *y; x=5; y=&x; *y++; x--; cout << x;

9th Jan 2021, 8:55 PM
Edward Finkelstein
Edward Finkelstein - avatar
4 Respostas
+ 5
*y++; <-- your incrementing the pointer...your NOT incrementing the value it's pointing to. try: (*y)++.
9th Jan 2021, 9:45 PM
rodwynnejones
rodwynnejones - avatar
0
Because x-- will decremented it's value by 1 so it is 4.
9th Jan 2021, 9:10 PM
HBhZ_C
HBhZ_C - avatar
0
but why doesn’t incrementing y increment x? y stores x’s adress, so why doesn’t x increment as well?
9th Jan 2021, 9:17 PM
Edward Finkelstein
Edward Finkelstein - avatar
0
ah ok, thanks rodwynnejones
9th Jan 2021, 11:24 PM
Edward Finkelstein
Edward Finkelstein - avatar