Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 多维数组的类型定义?_C_Multidimensional Array_Typedef - Fatal编程技术网

C 多维数组的类型定义?

C 多维数组的类型定义?,c,multidimensional-array,typedef,C,Multidimensional Array,Typedef,这意味着什么。如果我们有这样一个类型定义,会发生什么。这是我的面试问题 你会得到诊断 int[x][]是一个不完整的数组类型,无法完成。您将得到一个编译错误。对于多维数组,最多可以省略第一个维度。因此,例如,int-array[][x]将是有效的。假设您有: typedef int array [x][]; 正如其他人指出的,typedefint数组[3][]将不会编译。只能忽略数组长度中最重要的(即第一个)元素 但你可以说: 这意味着array是长度为3个数组的int数组(长度尚未指定) 要

这意味着什么。如果我们有这样一个类型定义,会发生什么。这是我的面试问题

你会得到诊断


int[x][]
是一个不完整的数组类型,无法完成。

您将得到一个编译错误。对于多维数组,最多可以省略第一个维度。因此,例如,
int-array[][x]
将是有效的。

假设您有:

typedef int array [x][];
正如其他人指出的,
typedefint数组[3][]将不会编译。只能忽略数组长度中最重要的(即第一个)元素

但你可以说:

这意味着
array
是长度为3个数组的int数组(长度尚未指定)

要使用它,需要指定长度。您可以使用如下初始化器来完成此操作:

typedef int array [][3];
但你不能说:

array A = {{1,2,3,},{4,5,6}};   // A now has the dimensions [2][3]
在这种情况下,
A
的第一个维度没有指定,因此编译器不知道要为它分配多少空间

请注意,在函数定义中使用此
数组
类型也很好,因为编译器总是将函数定义中的数组转换为指向其第一个元素的指针:

array A; 
请注意,在这种情况下:

// these are all the same
void foo(array A);
void foo(int A[][3]);
void foo(int (*A)[3]); // this is the one the compiler will see
编译器仍然可以看到

void foo(int A[10][3]); 
因此,
A[10][3]
10
部分被忽略

总之:

void foo(int (*A)[3]);

有关typedef解析的更多详细信息:
void foo(int (*A)[3]);
typedef int array [3][]; // incomplete type, won't compile
typedef int array [][3]; // int array (of as-yet unspecified length) 
                         // of length 3 arrays