在我阅读 ISO:IEC 9899:TC3 6.7.5.2 数组声明符 -> 10 示例 4 之后。 我想知道我以前从未见过具有这种有用结构的代码。
我编写了这个简单的示例代码来测试我是否正确理解了它的工作原理。
int m = 9;
int foo (int iArray[m], int n);
int main(int argc, char **argv)
{
int iArray[m];
int n = 5;
iArray[n] = 555;
printf ("%d\r\n", foo (iArray, n));
return 0;
}
int foo (int iArray[m], int n)
{
int iLocalArray[n];
iLocalArray[n - 1] = iArray[n];
return iLocalArray[n - 1];
}
当我尝试在 MSVC2010 上编译此代码时......当然无法编译。 正如我们所知,MSVC2013 之前没有任何真正的 C 微软编译器。 但是好吧,所以我安装了 MSVC2013RC 并想,它应该像他们所说的那样运行 MSVC2013 包含一个真正的 C99 编译器。当我开始编译时,仍然是同样的错误:
1>[...].c(6): error C2057: expected constant expression
1>[...].c(6): error C2466: cannot allocate an array of constant size 0
1>[...].c(11): error C2057: expected constant expression
1>[...].c(11): error C2466: cannot allocate an array of constant size 0
1>[...].c(11): error C2133: 'iArray' : unknown size
1>[...].c(20): error C2057: expected constant expression
1>[...].c(20): error C2466: cannot allocate an array of constant size 0
1>[...].c(22): error C2057: expected constant expression
1>[...].c(22): error C2466: cannot allocate an array of constant size 0
1>[...].c(22): error C2133: 'iLocalArray' : unknown size
但是对于一个被宣布为第一个甚至尊重 C99 标准的微软编译器的编译器来说,这是非常奇怪的错误,不是吗?或者我只是错误地使用了可变长度数组并使用了错误的语法?
最佳答案
如果您阅读了 MSVC 文档(可能会更改),那么它会声明 MSVC 2013 conforms to C90和 reference for declarations没有提到 VLA。还有一个roadmap for MSVC这表示他们采用了 C99 的战术元素,因此不一定支持完整标准。
因此,VLA 似乎仍然不是 MSVC 2013 中受支持的 C99 子集的一部分。
关于c - 变长数组;另一个构造 MSVC 无法编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18956599/