difference between pointer to an array and pointer to the first element of an array

后端 未结 6 1365
时光取名叫无心
时光取名叫无心 2020-12-05 21:42

int (*arr)[5] means arr is a pointer-to-an-array of 5 integers. Now what exactly is this pointer?

Is it the same if I declare int arr

6条回答
  •  被撕碎了的回忆
    2020-12-05 22:17

    If you write int arr[5], you are creating an array of five int on the stack. This takes up size equal to the size of five ints.

    If you write int (*arr)[5], you are creating a pointer to an array of five int on the stack. This takes up size equal to the size of a pointer.

    If it is not clear from the above, the pointer has separate storage from the array, and can point at anything, but the array name cannot be assigned to point at something else.

    See my answer here for more details.

提交回复
热议问题