c和c ++中2d数组元素的类型是什么?

时间:2021-02-06 19:33:54

e.g.

int arr[2][3] = ...

The type of arr[0] is

arr [0]的类型是

int (*)[3] // pointer to int[3], which is a pointer. 

Or

int[3] // an array whose size is 3, which is an array. 

Google tells me nothing about the question.

谷歌没有告诉我这个问题。

I know pointer and array are different types(derived types).

我知道指针和数组是不同的类型(派生类型)。

Maybe C and C++ treat it differently, I hope to see standard wording.

也许C和C ++对待它的方式不同,我希望看到标准的措辞。

1 个解决方案

#1


15  

arr[0] is of type int [3] which is not a pointer.

arr [0]的类型为int [3],它不是指针。

int (*p)[3] is of type int(*)[3] meaning pointer to an array of 3 elements.

int(* p)[3]的类型为int(*)[3],表示指向3个元素的数组。

Pointer is not array and array is not pointer.

指针不是数组,数组不是指针。

Now when you pass this 2d array to a function (or any case where decaying occurs) then it decays into pointer to the first element which is int (*)[3].

现在当你将这个2d数组传递给一个函数(或任何发生衰减的情况)时,它会衰减成指向第一个元素的指针,即int(*)[3]。

To be more clear in C 2d array is nothing but array of arrays.

在C 2d数组中更清楚的只是数组数组。

Dissecting

  • arr is an array each of element of which is again an array with 3 elements.

    arr是一个数组,每个元素都是一个包含3个元素的数组。

  • arr[0] in most of the cases (except sizeof etc) will decay into pointer to first element it contains which is an int*.

    在大多数情况下(除了sizeof等),arr [0]将衰减为指向它包含的第一个元素的指针,这是一个int *。

  • arr[0][0] is an int.

    arr [0] [0]是一个int。

  • At last &arr[0] .. guess what? This is of type int(*)[3].

    最后&arr [0] ..猜怎么着?这是int(*)[3]类型。

#1


15  

arr[0] is of type int [3] which is not a pointer.

arr [0]的类型为int [3],它不是指针。

int (*p)[3] is of type int(*)[3] meaning pointer to an array of 3 elements.

int(* p)[3]的类型为int(*)[3],表示指向3个元素的数组。

Pointer is not array and array is not pointer.

指针不是数组,数组不是指针。

Now when you pass this 2d array to a function (or any case where decaying occurs) then it decays into pointer to the first element which is int (*)[3].

现在当你将这个2d数组传递给一个函数(或任何发生衰减的情况)时,它会衰减成指向第一个元素的指针,即int(*)[3]。

To be more clear in C 2d array is nothing but array of arrays.

在C 2d数组中更清楚的只是数组数组。

Dissecting

  • arr is an array each of element of which is again an array with 3 elements.

    arr是一个数组,每个元素都是一个包含3个元素的数组。

  • arr[0] in most of the cases (except sizeof etc) will decay into pointer to first element it contains which is an int*.

    在大多数情况下(除了sizeof等),arr [0]将衰减为指向它包含的第一个元素的指针,这是一个int *。

  • arr[0][0] is an int.

    arr [0] [0]是一个int。

  • At last &arr[0] .. guess what? This is of type int(*)[3].

    最后&arr [0] ..猜怎么着?这是int(*)[3]类型。