in this code:
int* a;
int* b;
int c;
int* d;
a=new int(5);
b=&(*a);
c=*a;
d=&c;
cout<<"*a = "<<*a<<endl;
cout<<"a = "<<a<<endl;
cout<<"b ="<<b<<endl;
cout<<"d = "<<d<<endl;
I get:
*a = 5
a = 0x83a2008
b =0x83a2008
d = 0xbfbfe540
why is d different from b? Aren’t they both &(*a) ? how can I get the d result with a single line?
Thanks a lot.
apoints to a dynamically allocated location, holding the value 5. When you doc = *a;, you’re copying the value 5 from that dynamically allocated location intoc. You’re then taking the address ofcand assigning it tod(and printing it out).When you end up with is something like this:
The solid lines indicate a pointer referring to a location. The dashed line indicates movement of data.