C多维数组是否连续没有孔?

时间:2022-02-19 21:33:47

I am unable to find in the C standard docs specifically where it says that multidimensional arrays are contiguous. While it can be implied from the fact that array elements are contiguous, I want some perspective from the community.

我无法在C标准文档中找到它所说的多维数组是连续的。虽然数组元素是连续的这一事实可以暗示,但我想从社区中获得一些观点。

The following code prints out the numbers in the order that I would expect, which is 1 - 9.

以下代码按照我期望的顺序打印出数字,即1 - 9。

#include <stdio.h>

int main()
{
    int a[][3] = {{1,2,3},{4,5,6},{7,8,9}};
    int* p = (int*)a;
    int i;

    for (i = 0; i < sizeof(a)/sizeof(int); i++)
        printf("%d ",p[i]);

    return 0;
}

4 个解决方案

#1


10  

Yes, it can be obtained by induction. (Just to add, as a suggestion, if that helps, try to think of multi-dimensional arrays as array of arrays.)

是的,它可以通过归纳获得。 (只是作为建议添加,如果有帮助,尝试将多维数组视为数组数组。)

For example, consider an array like a[3][3].

例如,考虑像[3] [3]这样的数组。

  • So, a[0][0], a[0][1] and a[0][2] are elements of a[0] and they will be contiguous.

    因此,[0] [0],[0] [1]和[0] [2]是[0]的元素,它们将是连续的。

  • Next, a[0] and a[1] are elements of a, so it will be contiguous

    接下来,[0]和[1]是a的元素,因此它将是连续的

an so on.

等等。

Taken together, a[0][2] and a[1][0] will be residing next to each other, thereby continuing the contiguity.

总之,[0] [2]和[1] [0]将彼此相邻,从而继续连续性。

For better visual representation, see the below illustration.

有关更好的视觉表示,请参见下图。

The array, say int arr[4][5], has four rows, a[0],a[1], a[2] and a[3] and they are contiguous.

数组,比如int arr [4] [5],有四行,a [0],a [1],a [2]和[3],它们是连续的。

Now each of those rows have five columns, like a[n][0], a[n][1], a[n][2], a[n][3], a[n][4] and they are contiguous.

现在这些行中的每一行都有五列,如[n] [0],[n] [1],[n] [2],[n] [3],[n] [4]和他们是连续的。

So, the all the elements (and elements of elements) of the array are contiguous.

因此,数组的所有元素(和元素元素)都是连续的。

C多维数组是否连续没有孔?

#2


6  

Yes they are contiguous. I would say the fact "an array" (i.e. singular) is contiguous infers that a multi-dimensional one is. Each array within it must be contiguous and the outer array must be a contiguous collection of those arrays...

是的,他们是连续的。我会说事实“一个数组”(即单数)是连续的,推断出多维数据。其中的每个数组必须是连续的,外部数组必须是这些数组的连续集合......

#3


6  

According to 6.2.5 Types p20:

根据6.2.5类型p20:

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. ...

数组类型描述了具有特定成员对象类型的连续分配的非空对象集,称为元素类型。只要指定了数组类型,元素类型就应该是完整的。 ...

Therefore all array types, multidimensional or not, are contiguously allocated.

因此,所有数组类型(多维或非多维)都是连续分配的。

#4


2  

C does not explicitly have multi-dimentional arrays, C have array of arrays, and arrays in C are contiguously represented in memory. Hence all arrays in C are contiguous.

C没有明确地具有多维数组,C具有数组数组,并且C中的数组在存储器中连续表示。因此,C中的所有数组都是连续的。

#1


10  

Yes, it can be obtained by induction. (Just to add, as a suggestion, if that helps, try to think of multi-dimensional arrays as array of arrays.)

是的,它可以通过归纳获得。 (只是作为建议添加,如果有帮助,尝试将多维数组视为数组数组。)

For example, consider an array like a[3][3].

例如,考虑像[3] [3]这样的数组。

  • So, a[0][0], a[0][1] and a[0][2] are elements of a[0] and they will be contiguous.

    因此,[0] [0],[0] [1]和[0] [2]是[0]的元素,它们将是连续的。

  • Next, a[0] and a[1] are elements of a, so it will be contiguous

    接下来,[0]和[1]是a的元素,因此它将是连续的

an so on.

等等。

Taken together, a[0][2] and a[1][0] will be residing next to each other, thereby continuing the contiguity.

总之,[0] [2]和[1] [0]将彼此相邻,从而继续连续性。

For better visual representation, see the below illustration.

有关更好的视觉表示,请参见下图。

The array, say int arr[4][5], has four rows, a[0],a[1], a[2] and a[3] and they are contiguous.

数组,比如int arr [4] [5],有四行,a [0],a [1],a [2]和[3],它们是连续的。

Now each of those rows have five columns, like a[n][0], a[n][1], a[n][2], a[n][3], a[n][4] and they are contiguous.

现在这些行中的每一行都有五列,如[n] [0],[n] [1],[n] [2],[n] [3],[n] [4]和他们是连续的。

So, the all the elements (and elements of elements) of the array are contiguous.

因此,数组的所有元素(和元素元素)都是连续的。

C多维数组是否连续没有孔?

#2


6  

Yes they are contiguous. I would say the fact "an array" (i.e. singular) is contiguous infers that a multi-dimensional one is. Each array within it must be contiguous and the outer array must be a contiguous collection of those arrays...

是的,他们是连续的。我会说事实“一个数组”(即单数)是连续的,推断出多维数据。其中的每个数组必须是连续的,外部数组必须是这些数组的连续集合......

#3


6  

According to 6.2.5 Types p20:

根据6.2.5类型p20:

An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. ...

数组类型描述了具有特定成员对象类型的连续分配的非空对象集,称为元素类型。只要指定了数组类型,元素类型就应该是完整的。 ...

Therefore all array types, multidimensional or not, are contiguously allocated.

因此,所有数组类型(多维或非多维)都是连续分配的。

#4


2  

C does not explicitly have multi-dimentional arrays, C have array of arrays, and arrays in C are contiguously represented in memory. Hence all arrays in C are contiguous.

C没有明确地具有多维数组,C具有数组数组,并且C中的数组在存储器中连续表示。因此,C中的所有数组都是连续的。