1

Possible Duplicate:
C: How come an array’s address is equal to its value?
C pointer : array variable

Considering a multidimensional Array:

int c[1][1];

Why all of the following expression points to the same address??

printf("%x", (int *) c);      // 0x00000500
printf("%x", *c);             // 0x00000500
printf("%x", c);              // 0x00000500

How would a pointer's actual value and it's derefernced value can be the same?

Community
  • 1
  • 1
zeronone
  • 2,912
  • 25
  • 28
  • Check out this link: http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c – Jason Oct 10 '12 at 17:00
  • You are not actually dereference the pointer with *c, to dereference it you need to use `*c[0]`. You can test this by initilizing to zero and trying the two "dereferences". – Youssef G. Oct 10 '12 at 17:02
  • @YoussefG. Arrays decay to pointers to the first element of the array, so `c` in an expression ends up pointing to the first element, and `*c` dereferences that value – Jason Oct 10 '12 at 17:05
  • @Jason I understand what Arrays are, but in a 2d array *c is not going to give you the first value, since that only dereferences 1 address: pointer to a pointer to a character, he is getting the second pointer's address, which just so happens to be the same address as the first pointer. Try a quick program and you will see *c will evalute to an address, but `**c` will evalute to zero (if you initilize to zero). – Youssef G. Oct 10 '12 at 17:08
  • @YoussefG.Sorry, I guess we're saying the exact same thing ... because this is a 2D-array, `*c` dereferences to another array. So the first element of `c` is an array, and by deferencing `c` you will return that array, but it too must decay to a pointer to its first element, which is the actual `int` element. I was using "value" in a generic sense of whatever that type is, whether it's an array, etc., not "value" as-in the value of the `int`. – Jason Oct 10 '12 at 17:12
  • @zeronone Okay, from a compiler standpoint, `c` is a 2d-array, and it decays to a pointer to it's first "element" ... that "first element", since c is of type `int (*)[1][1]`, will be an `int (*)[1]`. Since that first element's type is also an array-type, it too will decay to a pointer to its first element, which in this case is an `int`. – Jason Oct 10 '12 at 17:16
  • @Jason, yes makes sense now. also, correction on my previous comment, its a pointer to an integer not a char. – Youssef G. Oct 10 '12 at 17:22
  • @zeronone BTW, just to clear things up, here is a quick example compiled in C++ (which enforces type-saftey) of what I'm talking about: http://ideone.com/YdVDH – Jason Oct 10 '12 at 17:24

3 Answers3

1

You just have to think: where is the first position on this array?

Suppose it's on 0x00000050 in your memory space. What is the first item in your array? It's c[0][0], and its address is 0x00000050. Sure enough, the address of the first position is the same of the array. Even if you do c[0] only, it still points to the same address, as long as you cast it to the right type.

But you should not confuse pointers to arrays.

Toribio
  • 3,963
  • 3
  • 34
  • 48
1

Under most circumstances1, an expression of type "N-element array of T" will be converted ("decay") to expression of type "pointer to T", and the value of the expression will be the address of the first element in the array.

The expression c has type int [1][1]; by the rule above, the expression will decay to type int (*)[1], or "pointer to 1-element array of int", and its value will be the same as &c[0]. If we dereference this pointer (as in the expression *c), we get an expression of type "1-element array of int", which, by the rule above again, decays to an expression of type int *, and its value will be the same as &c[0][0].

The address of the first element of the array is the same as the address of the array itself, so &c == &c[0] == &c[0][0] == c == *c == c[0]. All of those expressions will resolve to the same address, even though they don't have the same types (int (*)[1][1], int (*)[1], int *, int (*)[1], int *, and int *, respectively).


1 - the exceptions to this rule are when the array expression is an operand of the sizeof, _Alignof, or unary & operators, or is a string literal being used to initialize another array in a declaration
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

How would a pointer's actual value and it's derefernced value can be the same
It's not a pointer, it's an array.

See Q&A #3 & #4

c is the address of the array. c is also the address of the first element.

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177
  • This answer doesn't really follow the question. The error is in the fact that he is dereferencing only one address. It has nothing to do with getting the address of a pointer. c is the address of a pointer to an array of 1 integer. – Youssef G. Oct 10 '12 at 17:22
  • @YoussefG. - The part of the OP's question that I was addressing is what I highlighted. Clearly OP is confused why a deferenced array is acting differently from a deferenced pointer (confusion regarding arrays vs. pointers) – Mike Oct 10 '12 at 17:24
  • Very loosely, I think you would be better off demonstrating the difference between 'int **c;' and 'int c[1][1];' to get that point across. – Youssef G. Oct 10 '12 at 17:30