21

Assume that

int array[16];

There is standard conversion called array-to-pointer conversion, so array would be converted implicitly to type int*, but why &array is equal to array?

For instance,

int array[16];
void *p = array;
void *q = &array;
printf("%p\n", p);
printf("%p\n", q);

This will give out the same address and no compiling error.

Why?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
  • 1
    You can also check this link : https://www.google.com/amp/s/www.geeksforgeeks.org/whats-difference-between-array-and-array-for-int-array5/amp/ – Amin Rostami Feb 25 '19 at 10:51

4 Answers4

21

The type of &array is int (*)[16] (a pointer to an array of 16 integers). The type of array, when left to decay, is int* (a pointer to an integer). They both point to the same location, but have a different meaning to the compiler.

If you do (&array)[0], the value you end up with is the original array of 16 integers, that you can subscript again, like (&array)[0][0]. (&array)[1] would be the next array of 16 integers, if there was one.

If you do array[0], the value you end up with is an integer, which you can't subscript again. array[1] is just the next integer. (This is true regardless of if array is a int[16] or a int*.)

Obviously, if you then turn your pointers into void pointers, you lose any semantic difference there could have been.

zneak
  • 134,922
  • 42
  • 253
  • 328
10

That is how array names behave in C.

The name of the array variable represents the address of the first element of the array.

so,

void *p = array;   //array name, gives address of the  first element.

and

void *q = &array;  //adress-of-array name, also gives address of the first element
                   // actually &array is of type int (*)[16] which is decayed to int * 
                   // and casted to void * here

P.S. Even, FWIW,

 void *r = &array[0]; //address of the first element

will also give you the same address.

This will give out the same address and no compiling error.

They are indeed same value, and here compiler has nothing to scream about.

Point to note: once you assign the address(es) to a void pointer, you'll lose the type information associated with them.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
-1

An array in C behaves as a pointer to the address of the first element in memory.

Lydia Ralph
  • 1,455
  • 1
  • 17
  • 33
  • Not entirely true. In most cases, an array name decays into a pointer to the first element in the array, but take this example: `char foo[20] = "bar"; size_t size = sizeof foo;`, if `foo` would be a pointer, `size` would be 4 or 8, but in fact it will be 20 – Elias Van Ootegem May 12 '15 at 15:04
-2

Because you're basically saying the same thing in these two lines:

void *p = array; // Pointer to array, which is an address
void *q = &array; // Pointer to reference to an array, which is basically saying "A pointer to the same address as the pointer".

In other words:

void *p;

Is the same as:

&array;
Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35