简体   繁体   English

多维数组列宽

[英]Multidimensional Array column width

In passing a 2-D array in functions, why are we giving its column number as follows: 在函数中传递二维数组时,为什么要给出其列号如下:

int arr1[2][3] = {{1,2,3}, {4,5}};

void Array(const int [][3]); // prototype

Why we have to pass it column width? 为什么我们必须通过它列宽? If i keep it empty it gives an error! 如果我将其保留为空,则会出现错误!

A two dimensional array is in fact a one-dimensional array elements of which are in turn one dimensional arrays. 二维数组实际上是一维数组元素,而一维数组元素又是一维数组。

You can consider an array declaration like this 您可以考虑这样的数组声明

int arr1[2][3] = {{1,2,3},{4,5}};

the following way 以下方式

typedef int T[3];
T arr1[2] = {{1,2,3},{4,5}};

When you pass an array by value as an argument to a function it is converted to pointer to its first element. 当按值传递数组作为函数的参数时,它将转换为指向其第一个元素的指针。

So if you have a function like this 所以如果你有这样的功能

void Array(const T arr[] );

then this declaration is equivalent to 那么这个声明相当于

void Array(const T *arr );

and the both declare the same one function. 并且两者都声明相同的一个功能。

It is important to know the complete type T that you can use the pointer arithmetic with the pointer. 重要的是要知道可以将指针算术与指针一起使用的完整类型T。 For example when you use expression ++arr then it means that the current value in arr is increased by sizeof( T ) . 基于使用表达例如++arr那么就意味着,在当前的值arr增加sizeof( T ) So the compiler need to know sizeof( T ). 因此,编译器需要知道sizeof(T)。 Otherwise it will be unable to do the pointer arithmetic. 否则它将无法执行指针算术运算。

So returning to your example the compiler need to know the type of element of the two dimensional array that is that its type T is int[3] 因此,回到您的示例,编译器需要知道二维数组的元素类型,即其类型Tint[3]

So this decleration 所以这个清白

void Array(const int [][3]); 

is equivalent to 相当于

void Array(const int ( * )[3]); 

or to the following 或以下

typedef int T[3];
void Array(const T * );

In this case the compiler will know the size of the object (the first element of the passed array) pointed to by its parameter that is by the pointer. 在这种情况下,编译器将知道由其参数(即指针)指向的对象(传递的数组的第一个元素)的大小。

One thing I noticed is that this initialization looks supicious: 我注意到的一件事是,这种初始化看起来很可笑:

int arr1[2][3] = {{1,2,3}, {4,5}};

arr1 has 2 rows and columns. arr1有2行和列。 Why is there no initialization of the 3rd column of row 2? 为什么第2行的第3列没有初始化? (FYI the compiler will make it zero, but I'm not sure if that was the intent.) (仅供参考,编译器会将其设置为零,但是我不确定这是否是意图。)

In C, a multidimensional array is represented as a "flattened" 1 dimensional array. 在C语言中,多维数组表示为“扁平”一维数组。 This is explained here: How Are C Arrays Represented In Memory? 此处说明: C数组如何在内存中表示?

In a 2 dimensional array the elements are stored in row major order. 在二维数组中,元素以行主顺序存储。 So these 2 declarations are conceptually the same: 因此,这两个声明在概念上是相同的:

int arr2[ROWS][COLS];
int arr1[ROWS*COLS];

And these 2 accesses are conceptually the same: 这两个访问在概念上是相同的:

arr2[i][j]
arr1[(i*COLS)+j]

In in fact, in C, the second form is what's happening under the hood. 实际上,在C语言中,第二种形式是实际情况。 This explains why your prototype needs to know how many columns - it's so the compiler can perform the multiplication to get the proper element in the linear array. 这就解释了为什么您的原型需要知道多少列-这样编译器就可以执行乘法运算以获取线性数组中的适当元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM