+ 7

How do we arrive at 26 as answer here? int a = 10; int b = 13; int *p1= &a; int *p2= &a; *p1 = b; cout << (*p1) + (*p2);

I failed this question in a challenge, hoping the community can shed light.

3rd Mar 2019, 10:13 AM
david chongo
david chongo - avatar
6 odpowiedzi
+ 6
a = 10 b = 13 *p1 = &a - > store address of a *p2 = &a - > store address of a *p1 = b - > the value of a is now equal to the value of b cout << *p1 + *p2; - > p1 and p2 point to a, the Asterix means take the value at address of a (so 13) So 13 + 13 = 26
3rd Mar 2019, 10:34 AM
Théophile
Théophile - avatar
+ 4
Ah I see! So since pointer *p1 has now been assigned value of 13, which replaces the 10, and because all the two pointers point to same value of 13, the operation (*p1)+(*p2)= 26. Thanks a lot!
3rd Mar 2019, 10:48 AM
david chongo
david chongo - avatar
+ 1
You're welcome!
3rd Mar 2019, 11:26 AM
Théophile
Théophile - avatar
+ 1
Hii
20th May 2019, 11:47 AM
Shuva Jit Gayen
Shuva Jit Gayen - avatar
0
why value two not equal to one
9th May 2019, 5:28 AM
Bianca
Bianca - avatar
0
(*p2)+(*p1)=20 so 10 +10 why not.why first like two 10 13 is 13+13 why not 13 to 10 like 10 +10
9th May 2019, 5:29 AM
Bianca
Bianca - avatar