The other answers are correct, but none of them emphasize the idea that it is possible for all three to contain the same value, and so they're in some way incomplete.
The reason this can't be understood from the other answers is that all the illustrations, while helpful and definitely reasonable under most circumstances, fail to cover the situation where the pointer x
points to itself.
This is pretty easy to construct, but clearly a bit harder to understand. In the program below, we'll see how we can force all three values to be identical.
NOTE: The behavior in this program is undefined, but I'm posting it here purely as an interesting demonstration of something that pointers can do, but shouldn't.
#include <stdio.h>
int main () {
int *(*x)[5];
x = (int *(*)[5]) &x;
printf("%p\n", x[0]);
printf("%p\n", x[0][0]);
printf("%p\n", x[0][0][0]);
}
This compiles without warnings in both C89 and C99, and the output is the following:
$ ./ptrs
0xbfd9198c
0xbfd9198c
0xbfd9198c
Interestingly enough, all three values are identical. But this shouldn't be a surprise! First, let's break down the program.
We declare x
as a pointer to an array of 5 elements where each element is of type pointer to int. This declaration allocates 4 bytes on the runtime stack (or more depending on your implementation; on my machine pointers are 4 bytes), so x
is referring to an actual memory location. In the C family of languages, the contents of x
are just garbage, something left over from previous usage of the location, so x
itself doesn't point anywhere—certainly not to allocated space.
So, naturally, we can take the address of the variable x
and put it somewhere, so that's exactly what we do. But we'll go ahead and put it into x itself. Since &x
has a different type than x
, we need to do a cast so we don't get warnings.
The memory model would look something like this:
0xbfd9198c
+------------+
| 0xbfd9198c |
+------------+
So the 4-byte block of memory at the address 0xbfd9198c
contains the bit pattern corresponding to the hexadecimal value 0xbfd9198c
. Simple enough.
Next, we print out the three values. The other answers explain what each expression refers to, so the relationship should be clear now.
We can see that the values are the same, but only in a very low level sense...their bit patterns are identical, but the type data associated with each expression means their interpreted values are different.
For instance, if we printed out x[0][0][0]
using the format string %d
, we'd get a huge negative number, so the "values" are, in practice, different, but the bit pattern is the same.
This is actually really simple...in the diagrams, the arrows just point to the same memory address rather than to different ones. However, while we were able to force an expected result out of undefined behavior, it is just that—undefined. This isn't production code but simply a demonstration for the sake of completeness.
In a reasonable situation, you will use malloc
to create the array of 5 int pointers, and again to create the ints that are pointed to in that array. malloc
always returns a unique address (unless you're out of memory, in which case it returns NULL or 0), so you'll never have to worry about self-referential pointers like this.
Hopefully that's the complete answer you're looking for. You shouldn't expect x[0]
, x[0][0]
, and x[0][0][0]
to be equal, but they could be if forced. If anything went over your head, let me know so I can clarify!
x[0]
,x[0][0]
andx[0][0][0]
have different types. They cannot be compared. What do you mean by!=
?int **x[5]
is an array of 5 elements. An element is a pointer to pointer to int`int** x[5]
would be an array of five pointers that point to pointers that point to int.int *(*x)[5]
is a pointer to an array of five pointers that point to int.x[0] != x[0][0] != x[0][0][0]
mean? This is not a valid comparison in C++. Even if you split it up intox[0] != x[0][0]
andx[0][0] != x[0][0][0]
it is still not valid. So, what does your question mean?