views:

258

answers:

5
int *w;
int **d;

d = &w;

What does the **d store exactly?

+4  A: 

After the assignment, **d is the same as *w. d is a pointer to a pointer to an integer; the pointer to an integer that it points to is w. So *d is w, and **d is *w.

Michael E
No, `**d` is *not* the same as `*w` -- at least they do not have the same values. Although I think I know what you mean to say (they both ultimately point to the same variable, I think that's slightly misleading.
Noldorin
@Noldorin, care to point to the C++ standards documentation which states that a dereferenced pointer is not the same value as the memory it points to?
Franci Penov
I think this answer is confusing, since it talks about the dereferencing operator, but the original code did not dereference anything. The pointer declaration qualifier happens to use the same * glyph. I believe that is what Noldrin is referring to. The two declarations are not equivalent, dereferencing d twice is however the same as dereferencing w once, but I don't believe that that is a useful answer in this context and wonder that it was marked 'accepted'.
Clifford
@Franci: I think you're misunderstanding my comment. Also, we're talking about C here, not C++. :)@Clifford: That's exactly what I'm referring to... I agree this answer creates confusion around the matter. If the OP is reading this, I urge you to at least read my answer or Jack Lloyd's (which is essentially correct).
Noldorin
+3  A: 

int ** represents 'a pointer to a pointer to an int' (also known as a double pointer).

Now, int *w simply represents a pointer to an int, thus the assignment d = &w is saying: "assign the address of w (which is itself a pointer/address) to d".

Noldorin
Why the down-vote, please?
Noldorin
+3  A: 

The value of **d is the same as the value of *w; *d is equal to the pointer value saved in w; because d is a pointer to a pointer to an int, you have to dereference it twice to get the actual value.

Jack Lloyd
+3  A: 

w stores the address of an int. d stores the address of a pointer to int (except in this case it stores a random value because it doesn't get assigned), in this case the address of d.

sepp2k
A: 

**d is a pointer to a pointer to an int, so **d would have the address of the pointer *w, when u say d=&w, but unless u have said that d=&w and just stated int *w int **d, it would have no meaning except for: int *w is a pointer to an int and int **d is a pointer to a pointer to an int, but in no way it would have stated that d will store the addres of w.

Anand