C中不可能的多维数组
Impossible multi-dimensional array in C
我正在阅读一些关于 C 语言编程的介绍 material。
给出了以下示例:
int matrix[][] = {{ 1,2,3}, {5,6,7}};
由于 gcc(在我的例子中是 7.5.0)和 clang(在我的例子中是 6.0.0)没有编译,我尝试了几个 C 标准(c89、c90),但 none 有效。
是否有有效的 C 标准版本?
这在 C 中是不合法的。错误消息不言自明:
declaration of 'matrix' as multidimensional array must have bounds for
all dimensions except the first
您需要提供除第一个以外的所有尺寸:
int matrix[][3] = {{ 1,2,3}, {5,6,7}};
int matrix[][2][3] = {{{ 1,2,3}, {5,6,7}}, {{ 1,2,3}, {5,6,7}}};
/* etc etc*/
Is there a C standard version in which this is valid?
没有
让我们看看您的语法:
int matrix[][] = {{ 1,2,3}, {5,6,7}};
这里,在C language
中你需要声明除first one(which is optional)
之外的所有维度的大小,而这里你没有声明second dimension
,所以显然它会给出你 syntax error
因为它在 C
.
中无效
如果我们取一个变量 x
,那么在 generalized form
中我们可以说 correct syntax
是:
int matrix[][x] = {{1,2,3,...., x terms}, {1,2,3,....,x terms},.... upto your desired no. of terms};
以及您的数组:
int matrix[][3] = {{1,2,3},{5,6,7}};
下面给出了一些示例供您参考:
int matrix[] = {1,2,3};
int matrix[][4] = {{1,2,3},{4,5,6,7}};
int matrix[][4][5] = {{1,2},{3,4,5,6},{7,8,9,10,11}};
int matrix[][5][3] = {{1,2,3,4,5,6},{7,8,9,10,11},{12,13,14}};
现在,让我们来回答你的最后一个问题:
Is there any version of standard C which allows to do so?
答案是:No!
目前有 no such version
个 C
可用,这使我们能够这样做。这是一个 strict syntax convention
你 should follow
.
希望这个解释对您有所帮助!
C 旨在允许单程处理的可能性。给出如下声明:
int foo[][4] = {{1,2,3},{4,5,6,7}};
编译器可以在读取内容时开始将内容放入内存。当它找到第一个右括号时,它会知道它需要在 4 之前插入 space,因为它会知道它输出了 3 个值但内部维度是 4。如果构造改为:
int foo[][] = {{1,2,3},{4,5,6,7}};
编译器无法知道在 3 之后需要 space 直到它读取 7 之后,此时它已经将值 1 到 6 写入连续的存储位置.虽然某些实现可能可以想象地使用多遍扫描,因此它们可以扩展语言以适应此类结构,但我不希望任何编译器供应商期望收益可以证明成本是合理的。
我正在阅读一些关于 C 语言编程的介绍 material。 给出了以下示例:
int matrix[][] = {{ 1,2,3}, {5,6,7}};
由于 gcc(在我的例子中是 7.5.0)和 clang(在我的例子中是 6.0.0)没有编译,我尝试了几个 C 标准(c89、c90),但 none 有效。
是否有有效的 C 标准版本?
这在 C 中是不合法的。错误消息不言自明:
declaration of 'matrix' as multidimensional array must have bounds for all dimensions except the first
您需要提供除第一个以外的所有尺寸:
int matrix[][3] = {{ 1,2,3}, {5,6,7}};
int matrix[][2][3] = {{{ 1,2,3}, {5,6,7}}, {{ 1,2,3}, {5,6,7}}};
/* etc etc*/
Is there a C standard version in which this is valid?
没有
让我们看看您的语法:
int matrix[][] = {{ 1,2,3}, {5,6,7}};
这里,在C language
中你需要声明除first one(which is optional)
之外的所有维度的大小,而这里你没有声明second dimension
,所以显然它会给出你 syntax error
因为它在 C
.
如果我们取一个变量 x
,那么在 generalized form
中我们可以说 correct syntax
是:
int matrix[][x] = {{1,2,3,...., x terms}, {1,2,3,....,x terms},.... upto your desired no. of terms};
以及您的数组:
int matrix[][3] = {{1,2,3},{5,6,7}};
下面给出了一些示例供您参考:
int matrix[] = {1,2,3};
int matrix[][4] = {{1,2,3},{4,5,6,7}};
int matrix[][4][5] = {{1,2},{3,4,5,6},{7,8,9,10,11}};
int matrix[][5][3] = {{1,2,3,4,5,6},{7,8,9,10,11},{12,13,14}};
现在,让我们来回答你的最后一个问题:
Is there any version of standard C which allows to do so?
答案是:No!
目前有 no such version
个 C
可用,这使我们能够这样做。这是一个 strict syntax convention
你 should follow
.
希望这个解释对您有所帮助!
C 旨在允许单程处理的可能性。给出如下声明:
int foo[][4] = {{1,2,3},{4,5,6,7}};
编译器可以在读取内容时开始将内容放入内存。当它找到第一个右括号时,它会知道它需要在 4 之前插入 space,因为它会知道它输出了 3 个值但内部维度是 4。如果构造改为:
int foo[][] = {{1,2,3},{4,5,6,7}};
编译器无法知道在 3 之后需要 space 直到它读取 7 之后,此时它已经将值 1 到 6 写入连续的存储位置.虽然某些实现可能可以想象地使用多遍扫描,因此它们可以扩展语言以适应此类结构,但我不希望任何编译器供应商期望收益可以证明成本是合理的。