tags:

views:

157

answers:

5

Hi there, just want to ask a beginner question...

here, I made some code, for understanding the concept/basic of pointer:

int a=1;
int *b=&a;
int **c = &b;
int ***d = &c;

cout << &*(&*d) << endl;

can someone explain to me, why the &*(&*d) return address of "c" instead of address of "b"? I've also tried code like &*(&*(&*(&*(&*d)))), but it keep return address of "c"

Thanks a lot :)

+4  A: 

Because the &* cancels each other out. * dereference d which gives the value of c. And then & gives the address of c, or the value of d.

KTC
thankss a lottt
BobAlmond
+3  A: 

& and * cancel each other out. If you want to dereference a pointer, you just need a *. So, try **d.

Alok
of course...!!!
jjj
+1  A: 

Simple rule: Use * to access/provide the value, use & to access/provide the address.

KMan
A: 

* dereferences the pointer, and gives you the contents of where it points to. & is basically addressof.

When used at the same point they cancel each other out, &*d is equivalent to d if you want to print address of c you would need *d, for b you would need **dand for the data in a you would need ***d.

David Ashmore
A: 

If x is a variable, then &*x is not exactly equal to x. The former is an rvalue, while the latter is an lvalue. *&x on the other hand is always the same as x, because both expressions are lvalues.

FredOverflow