+ 6

Unexpected output c++ pointers

Why the output is such ? https://www.sololearn.com/post/108346/?ref=app In sololearn lesson, output is something else https://www.sololearn.com/learn/CPlusPlus/1630/

6th Jun 2019, 7:51 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
8 Respostas
+ 2
It just depends from compiler to compiler and pc to pc I guess. They may be the same number to you, but the actual address may be completely different due to https://en.wikipedia.org/wiki/Virtual_address_space gcc on my pc ( and sololearn too appearently ) always seems to give me the same address too, but vc++ gives me a different one each time. You can try your code here: https://rextester.com/l/cpp_online_compiler_visual This one does give a different address each time.
6th Jun 2019, 9:03 AM
Dennis
Dennis - avatar
+ 5
Anna I get same result each time in both SL editor and My PC
6th Jun 2019, 8:12 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 4
Anna I am 100% sure
6th Jun 2019, 8:50 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 4
@~swim~ Sorry, I can't directly tag you, as I am using PC now. I properly recompiled my code using key: F9 It rebuilded the binary file and ran my code. I am sure, the problem is not that. I guess, the output is such due to my PC || My Compiler. @Dennis The web compiler you shared link, I ran code in it. It gave me different results each time I ran. I finally understood why this happens. The whole game is of memory.
8th Jun 2019, 3:25 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 3
The & operator returns the memory address where the variable is stored. If you run the code multiple times on your pc, you'll get a different value each time.
6th Jun 2019, 8:00 AM
Anna
Anna - avatar
+ 2
SPACE That's interesting, are you sure? 🤔 The addresses on your pc might look very similar, but they should be different each time
6th Jun 2019, 8:27 AM
Anna
Anna - avatar
+ 2
Dennis I noticed him using GCC and I get different addresses each time with GCC and Ubuntu 😁 I didn't know that this might be dependent on the pc or compiler. Learning never stops I guess
6th Jun 2019, 9:12 AM
Anna
Anna - avatar
+ 1
Because you are printing address of scores not the value of scores
6th Jun 2019, 1:52 PM
Anuj Raghu
Anuj Raghu - avatar