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?

8
  • Check out this link: stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c Commented Oct 10, 2012 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". Commented Oct 10, 2012 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 Commented Oct 10, 2012 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). Commented Oct 10, 2012 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. Commented Oct 10, 2012 at 17:12

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

3 Comments

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.
@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)
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.