+ 1

This topic is not clear to me.

Can someone please explain this code? https://code.sololearn.com/cGWfWrWRPMe3/?ref=app

11th Jun 2017, 4:40 PM
Anil Yilmaz
Anil Yilmaz - avatar
7 odpowiedzi
+ 3
Basically y is another name for the variable x. If you know about pointers, this is a very similar thing. When y is declared &y=x, it is saying that the address of y is equal to x, meaning they are names for the same spot (address) in memory. Whenever you change y, you are changing the value at a certain space in memory, which x also happens to point at. I hope this explanation makes sense. Happy coding :)
11th Jun 2017, 7:09 PM
Zeke Williams
Zeke Williams - avatar
+ 3
X becomes 5 right after the declaration of y. int &y = x; y = 5; So now x and y are variable names for the address where the value at that address is 5. Then they become 7 due to the post-increment operator that is used twice.
11th Jun 2017, 9:11 PM
Zeke Williams
Zeke Williams - avatar
+ 3
The variable will still increment when it's 5 since the loop condition is less than OR EQUAL to 5. When x becomes 6, then the loop stops. The & symbol might be a bit confusing here because it's used in the declaration. Try this instead: int y, x; y = &x; // a pointer int* pX; pX = &x // or like this int* pY = y;
11th Jun 2017, 9:23 PM
Zeke Williams
Zeke Williams - avatar
0
Thanks for the first part.X value is 0.How can x and y become 5? Which side of the equation is reference for this case? Secodly, how does the x value become 7 after loop?
11th Jun 2017, 8:44 PM
Anil Yilmaz
Anil Yilmaz - avatar
0
By the way, &y means the adress of y in the memory, not the value itself, isn't it? The dereference of the pointer, *p, gives the value, not the adress.
11th Jun 2017, 9:04 PM
Anil Yilmaz
Anil Yilmaz - avatar
0
Loop operates for once, since x is already 5.by 6 , loop is over, no more increments? Still confused about &y, it is the adress of variable, not the value? I guess this & sign is something else than adress.
11th Jun 2017, 9:17 PM
Anil Yilmaz
Anil Yilmaz - avatar
0
Y is increased by 1 in the loop for once and become 6.X value is updated in the loop and become 6.Then it is increased by one after loop.Is it correct? If it is correct, it s interesting to see the x value is automatically updated.
15th Jun 2017, 7:23 AM
Anil Yilmaz
Anil Yilmaz - avatar