且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

声明多维数组时,为什么可以省略第一维,而忽略其他维?

更新时间:2023-11-30 22:02:52

必须提及2D数组的两个维,除非它在函数的参数中,或者如果存在初始化程序,则可以省略第一个维.

It is necessary to mention both dimensions of 2D arrays except when it is in function's parameter or if an initializer is present then first dimension can be omitted.

用作函数中的参数时,例如

When used as a parameter in a function, for example,

int 2D_arr[m][n]   

转换为

int (*2D_arr)[n]  

因此,第一维可以省略.但是,第二个维度必须在此告诉编译器指针2D_arr是指向 n ints 数组的指针.

Therefore, first dimension can be omitted. But, second dimension must be there to tell the compiler that the pointer 2D_arr is a pointer to an array of n ints.

在第二种情况下,如果存在初始值设定项

In second case, when initializer is present

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

编译器使用初始化程序的长度仅计算第一维.其余维度必须在声明时明确指定.

the compiler uses the length of the initializer to calculate the first dimension only. The rest of the dimension must be explicitly specified at the time of declaration.