0

Incrementing an address

int x = 10, y = 20; int *ptr =&x; int&ref = y; ptr++; ref++; cout<< x << y; // outputs 1021 Out of curiosity, since ptr++ doesn't affect the final output, what actually happens to the memory location of x after we incremented ptr?

26th Jul 2020, 6:30 AM
Solus
Solus - avatar
2 Respostas
+ 1
As i understand, nothing. Just ptr is now pointing on another memory location. They are different variables. You can check it by printing x address before and after incrementing
26th Jul 2020, 7:21 AM
Quarz
0
after ptr++, ptr will no more pointing to x. So x will not affect. ref++ result same as y++, Since ref storing the refference to y, so both points to same location that is value 20.so ref++ make y=21.
26th Jul 2020, 12:13 PM
Jayakrishna 🇮🇳