What's the difference between array and &array?

后端 未结 4 1783
感动是毒
感动是毒 2020-11-30 05:14

Assume that

int array[16];

There is standard conversion called array-to-pointer conversion, so array would be converted implic

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 05:36

    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.

提交回复
热议问题