+ 2

What is the output of this code?

int a = 42; int& p = a; p - = 8; cout << a;

3rd Apr 2018, 10:50 AM
Prafulla Kumar
2 Answers
+ 3
The answer is 34. '&' makes variable p as the reference to an int a. This means now, p and a points to the same memory location(But it's not that value of a is copied to p but p refers to memory address of a) Updating value of reference variable updates the value of original variable too as they point to same memory location. So p -= 8 is equivalent to a -=8 which is a = a - 8 producing 34 as the output
3rd Apr 2018, 11:39 AM
Bishal Sarang
Bishal Sarang - avatar
+ 3
a = 42 p = a p - = 8, p - 8 = a, 42 - 8 = a a = 34
3rd Apr 2018, 11:48 AM
✌️ RK ✌️
✌️ RK ✌️ - avatar