声明多维数组时,为什么可以省略第一维,而忽略其他维? [英] Why is it allowed to omit the first dimension, but not the other dimensions when declaring a multi-dimensonal array?

查看:144
本文介绍了声明多维数组时,为什么可以省略第一维,而忽略其他维?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不必提及多维数组的第一维,而不必提及其他维:

Why it is not necessary to mention first dimension of multidimensional array and necessary to mention other dimensions:

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

我无法理解其背后的概念或逻辑.

I am not able to understand the concept or logic behind this.

推荐答案

必须提及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.

这篇关于声明多维数组时,为什么可以省略第一维,而忽略其他维?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆