Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between array and &array?

Tags:

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?

like image 302
Kevin Dong Avatar asked May 12 '15 14:05

Kevin Dong


People also ask

What is difference between {} and [] in Javascript?

{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.

What is the difference between int array [] and int [] array?

What is the difference between int[] a and int a[] in Java? There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.

What is difference between array and list?

List is used to collect items that usually consist of elements of multiple data types. An array is also a vital component that collects several items of the same data type. List cannot manage arithmetic operations. Array can manage arithmetic operations.

What is diff between array and ArrayList?

Array is a fixed length data structure whereas ArrayList is a variable length Collection class. We cannot change length of array once created in Java but ArrayList can be changed. We cannot store primitives in ArrayList, it can only store objects. But array can contain both primitives and objects in Java.


2 Answers

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.

like image 55
zneak Avatar answered Sep 30 '22 13:09

zneak


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.

like image 24
Sourav Ghosh Avatar answered Sep 30 '22 13:09

Sourav Ghosh