I want to discuss this problem can someone understand me this problem? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

I want to discuss this problem can someone understand me this problem?

What is the output of this code? int x = 50; int &y = x; ++x; cout << ++x + y;

30th Aug 2020, 2:54 PM
Bayzid
Bayzid - avatar
2 Respostas
+ 3
x = 50 initial state y stores the address of x ++x; changes x to 51 and cout << ++x + y; here ++x will increment x again and making it 52 then y will call the x from the address. Which has 52 there as well So 52 + 52 = 104
30th Aug 2020, 2:59 PM
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ 
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ  - avatar
0
int &y = x; Here we create an int reference, and we initialize it by setting the reference to point to variable <x>. From this moment on, <y> behaves like an alias for <y>, whatever happens to <x> also happens to <y>. https://www.differencebetween.com/difference-between-pointer-and-vs-reference/
30th Aug 2020, 3:56 PM
Ipang