pointers

May 24, 2016 at 2:18pm
Assume you have the following layout of the memory. What is the output? Write your answer within the provided box. Assume there are no syntax errors.

#include<iostream>
#include<cstdlib> u
sing namespace std;
int main() {
int a = 10;
int *x = &a;
int *y;
int c[4] = {10, 11, 12, 13};
int *ptr = &c[1];
cout<<"ONE "<< &a <<"\n";
cout<<"TWO " << &x <<"\n";
cout<<"THREE "<< x <<"\n";
cout<<"FOUR "<<*x <<"\n";
cout<<"FIVE "<< &y <<"\n";
*x = *x + 5;
cout<<" SIX " <<*x<<"\n";
y = x;
*y = *x + 2;
cout<<"SEVEN "<<*y<<"\n";
ptr++;
cout<<"EIGHT "<< &ptr<<" \n";
cout<<"NINE " <<*ptr<<"\n ";
return 0; }


variable name: adress: 1024
variable name:a adress: 1028
variable name:x adress: 1032
variable name:y adress: 1036
variable name:c[0] adress: 1040
variable name:c[1] adress: 1044
variable name:c[2] adress: 1048
variable name:c[3] adress: 1052
variable name:ptr adress: 1056
variable name: adress: 1060


i've come across this question and got the output:

1028
1032
1028
10
1036
15
17
1056
11

Can anyone tell me if this is correct?
Last edited on May 24, 2016 at 2:20pm
May 24, 2016 at 2:24pm
The formatting of your post looks like it got screwed up a bit. Could you fix it?

As for your question, all of them look correct to me except the last one.
May 24, 2016 at 2:33pm
This is how it was formatted in the question /: sorry.
what would the last one be? 12?
Thanks!
Last edited on May 24, 2016 at 2:34pm
May 24, 2016 at 3:20pm
Explain why you think your previous answer is wrong and how you got your previous answer, it'll help me determine if you did it correctly.
May 24, 2016 at 5:51pm
*ptr is pointing the content of c[1] which is 11.
But it could be 12 because of the ptr++..i don't know honestly
May 24, 2016 at 9:01pm
Could be? Do you know what post-incrementing a pointer does? Take a look at whatever reference you are learning from and you should be able to puzzle out the answer.
Topic archived. No new replies allowed.