0

PLEASE HELP - Can someone please explain the output of this code?

https://code.sololearn.com/cRCcXLp8Y76s/?ref=app The output of this code is 1021. Can someone please explain why? Please be specific and go through all steps of the code. NOTE: I didn’t write this code. Credit goes to Naveen Kumar

21st Apr 2018, 4:32 PM
Jackson Meddows
Jackson Meddows - avatar
5 Answers
+ 3
you create ptr as pointer to x when you do ptr++ the pointer moves one place in memory and it points somewhere ... so x remains 10 you create ref as reference to y that means it behaves as if it was one variable changes in ref are done in y so y is incremented and it is 21
21st Apr 2018, 4:37 PM
michal
+ 3
yes, you only move the pointer away if you did (*ptr)++; it would increase x, because *ptr is the value ptr is a memory address
21st Apr 2018, 4:41 PM
michal
+ 1
So when it says ptr++ it doesn’t increase because changing pointer doesn’t affect the x?
21st Apr 2018, 4:39 PM
Jackson Meddows
Jackson Meddows - avatar
+ 1
Thanks a lot Michal!
21st Apr 2018, 4:42 PM
Jackson Meddows
Jackson Meddows - avatar
0
@michal can you please go to this link? https://www.sololearn.com/Discuss/1232575/please-explain-the-output-of-this-code It's another question that I have. Thanks!
22nd Apr 2018, 5:24 PM
Jackson Meddows
Jackson Meddows - avatar