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

后端 未结 6 1363
时光取名叫无心
时光取名叫无心 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:22

    The address of the whole array, and the address of the first element, are defined to be the same, since arrays in C++ (and C) have no intrinsic padding besides that of the constituent objects.

    However, the types of these pointers are different. Until you perform some kind of typecast, comparing an int * to an int (*)[5] is apples to oranges.

    If you declare arr[5], then arr is not a pointer to the first element. It is the array object. You can observe this as sizeof( arr ) will be equal to 5 * sizeof (int). An array object implicitly converts to a pointer to its first element.

    A pointer to an array does not implicitly convert to anything, which may be the other cause of your confusion.

提交回复
热议问题