0

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

I test in GCC 4.4.1 and I find &a=a. I can't understand it. I think &a should be the address where stores the address of the array, it can't be the same. Could someone give me a good explanation? Thanks very much.

Community
  • 1
  • 1
Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • 1
    "the address where stores the address of the array" - the array directly points to the location, there is no address where this address is stored.... – Karoly Horvath Oct 30 '12 at 14:30
  • It's not the same. It has the same value, but a different type. See http://stackoverflow.com/a/13043062/856199 – Nikos C. Oct 30 '12 at 14:32

7 Answers7

8

An array is an object in memory. It has an address and a size. It's also true that in certain contexts, an array decays into a pointer to its first element. So numerically, if both a and &a are compared as pointer values, they compare as equal, since they both point to the same address in memory. But they have different data types: a has type int[4] ("array 4 of int"), or int* ("pointer to int") in certain contexts, whereas &a always has type int (*)[4] ("pointer to array 4 of int").

 &a points here
 |
 V
+------+------+------+------+
| a[0] | a[1] | a[2] | a[3] |  sizeof(a) == 16
+------+------+------+------+
 ^
 |
 &a[0] also points here
 In certain contexts, 'a' means &a[0]

Hence, (void *)a == (void *)&a.

Also note that because a and &a point to different data types (and in particular, the pointed-to types have different sizes), doing pointer arithmetic will yield different results. a+1 will point to &a[1] (advances by one int value), whereas &a+1 will point to just past the end of the array at &a[4], since it advances by one "array 4 of int" unit:

                       &a+1 points here
                             |
                             V
+------+------+------+------+
| a[0] | a[1] | a[2] | a[3] |
+------+------+------+------+
        ^
        |
  a+1 points here
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 2
    However, their types are different -- `a` is "pointer to int", whereas `&a` is "pointer to array of four ints". So `a+1` and `(&a)+1` will be _different_ addresses numerically. – hmakholm left over Monica Oct 30 '12 at 14:32
  • OK, but you see, `(array)a` is value and that value is the address of the array,but it not in `a[0]`,`a[1]`,`a[2]` and `a[3]`. It should be stored in somewhere, but where it is? – Sayakiss Oct 30 '12 at 14:40
  • @Sayakiss: Depending on how the array is defined, its address isn't necessarily stored anywhere. If you define it on the stack, then its address is relative to the stack pointer or frame pointer, so when you write `printf("%p", a)`, the compiler computes `a` as something like `%ebp - 16`, e.g. – Adam Rosenfield Oct 30 '12 at 14:46
2

If a is declared as int a[4], then &a is a pointer with the type int *[4]( poointer to an array of 4 int).

When used in most expression contexts, a evaluates to a pointer to the first element of a, and has the type int*. The expression is exactly equivalent to &a[0]. This is required by the C99 standard in 6.3.2.1/3 "Lvalues, arrays, and function designators":

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

This is commonly known as decaying to a pointer; you'll hear it talked about as "a decays to a pointer".

So, the first element of an array has the same address as the array itself, by definition. So while &a and a have different types, they have the same pointer value.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
2

&a points to the address of the first element in the array(i.e a[0]).

a holds the address of the array.

And the address of the array(i.e a) is equal to the address of the first item in the array(i.e&a).

Sirko
  • 72,589
  • 19
  • 149
  • 183
Saurabh Rana
  • 3,350
  • 2
  • 19
  • 22
1

a is the array.

&a is the address of the array.

In most contexts, a used in an expression evaluates to the address of the first element of the array. Which of course is the same address as the address of the array, but with a different type. &a is not one of those contexts, but the a on the RHS of your comparison is.

For another example of two things that have the same value but with different type, consider that (int)1 == (char)1 and for that matter (int)1 == (float)1. Same numeric values, so they compare equal, but different types, so they don't behave the same for all purposes.

"It" doesn't store the address of the array anywhere that you're entitled to worry about. If a is an automatic variable, the compiler "knows" what offset a is at from the stack pointer, it doesn't necessarily store the address of a anywhere unless you tell it to. If it's a global variable, then its address may be stored in some symbol table somewhere, but your program doesn't directly access that location in the symbol table.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
0
int a[4];

a is an array 4 of int. Its type name is int [4].

&a is a pointer to array 4 of int. Its type name is int (*)[4].

Note that the expression &a = a is invalid in C: the left operand of the assignment operator has to be a modifiable lvalue but the result of the & operator is never a lvalue.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

The address of the array is equal to the address of the first item in the array.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
0

from wikipedia:

Array-pointer interchangeability

The "subscript" notation x[i] (where x designates a pointer) is a syntactic sugar for *(x+i).

so x = *(x+0)

wikipedia page

Community
  • 1
  • 1
Jarry
  • 1,891
  • 3
  • 15
  • 27