If p value is stored in a &q and q is increased then how p value will change? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

If p value is stored in a &q and q is increased then how p value will change?

p=10 &q=p ; q+=1; cout<<p<<q; as shown, p value is initialised to q and when q increases then i think it will not affect value of p. but the output is 1111

5th Jun 2017, 4:55 PM
Brutal😂
Brutal😂 - avatar
2 Answers
+ 18
increment in q affect p becoz q is storing the adress of p not the value that's why change in q means change in the value of p v address value p 1000 10 q 1002 1000
5th Jun 2017, 5:35 PM
Mansi Dagla
Mansi Dagla - avatar
+ 1
When declaring a variable with & you're passing a reference to the variable. So for most purposes, p and q will be the same I believe, although they are both seperate variables and will have different references. Think of it as being two paths to the same place.
5th Jun 2017, 5:30 PM
Smellfungus