What's the difference between array and &array?

后端 未结 4 1779
感动是毒
感动是毒 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:43

    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.

提交回复
热议问题