Question

What does this print to the screen? #include <iostream> using namespace std; int main() { int...

What does this print to the screen?

#include <iostream>

using namespace std;

int main()

{

int c = 5;

int *p = &c;

*p *= c;

cout << *(++p);

return 0;

}

I get 0 in Xcode, but it looks to me like it should be giving me the memory location. Please step me through the process.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Below is the answer with reference to image attached

Line 7: int c = 5;
we initialize variable c with value 5
Line 8 : int* p= &c;
p is an integer pointer it points to the address of variable of c
Line 9 : *p *= c;
This line actually means *p = *p * c which means multiplying value at address p with and storing it at address p
i.e storing (5*5 = 25 at p)

Line 10 : cout << *(++p);
since priority of () is greater than *, therefore ++p will take us to next address after p and *(++p) means printing the value
at ++p, which has currently no value so it will print a garbage value, which can be different everytime you run the code

Ouput for different runs

Thank you, please up vote.

Add a comment
Know the answer?
Add Answer to:
What does this print to the screen? #include <iostream> using namespace std; int main() { int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT