+ 1

why is the answer 17 in the following code

#include <iostream> using namespace std; int main() { int x = 5; int *p = &x; x = x + 4; x = *p + 4; *p = *p + 4; cout << x << endl << x << endl << *p << endl; return 0; } please help me!

26th Mar 2017, 11:52 AM
Splndid Nrzy
Splndid Nrzy - avatar
2 Answers
+ 10
p = &x means *p = x So, the core of the code is: x = x + 4 x = x + 4 x = x + 4 meaning x = 5 + 4 = 9 x = 9 + 4 = 13 x = 13 + 4 = 17 That's why the output is 17 17 17.
26th Mar 2017, 12:03 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
0
thank you for your answer!
26th Mar 2017, 12:44 PM
Splndid Nrzy
Splndid Nrzy - avatar