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

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.