+ 1

C++ - These mysteryous pointers

Why doesn't *y++ increment x? #include <iostream> using namespace std; int main() { int x, *y; x = 5; y = &x; *y++; cout << x << endl; x--; cout << x; return 0; }

26th Jul 2019, 6:37 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
5 Respuestas
+ 3
~ swim ~ yes i realised it, its pointing to another address. Indeed.
26th Jul 2019, 8:14 PM
Black Winter
+ 3
A pointer is a memory address that contains a value. If you increment a pointer, you go to the next address position. It does not increment the value contained in the pointer
27th Jul 2019, 9:45 AM
Da2
Da2 - avatar
+ 2
Yes, you can do ++*y; or, *y=*y+1; or (*y)++; Seems that pointers need first increment and then assignment. In second case *y=*y+1; all second part (*y+1) is assigned to *y. In case *y++; i suppose that first *y =*y and then +1 which is lost. I think its like typing *y+1; with no assignment or you just do this *(y+1) which is just another menory address. And thats why i hate pointers. :D
26th Jul 2019, 8:07 PM
Black Winter