show the output of the following C++ code, (assume the adress of x is 0012ff44)

int x=100;
int *ptr;
ptr= &x;
ptr=200;
cout<<x<<endl;
cout<<ptr<<endl;

Recommended Answers

Isn't that question for you to solve? We will help you hear, not give you the answers.

Jump to Post

All 2 Replies

Isn't that question for you to solve? We will help you hear, not give you the answers.

The address of 'x' won't change. The value of ptr may. First, ptr is assigned the address of x, then it is changed to '200'. IE, the results of the output values of 'x' and 'ptr' will differ. However, some systems will generate a core dump (segfault) when accessing the value of 'ptr' because it may point to protected memory - caveate programmer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.