2013-09-23 52 views
0

我读了ISO之后:IEC 9899:TC3 6.7.5.2数组声明符 - > 10示例4. 我想知道我以前从来没有见过这种有用结构的代码。可变长度数组;另一个构造MSVC不能编译?

我写了这个简单的示例代码来测试我知道它是如何工作的。

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的标准编译器相当奇怪的错误,不是吗?或者我只是使用变长数组错误,并使用错误的语法?

回答

2

如果您阅读MSVC文档(可能会有所更改),则说明MSVC 2013 conforms to C90reference for declarations未提及VLA。还有一个roadmap for MSVC表示他们正在采取C99的战术要素,因此不一定支持完整的标准。

因此看来,沃拉斯仍然不是C99支持的子集的一部分在2013年MSVC

+0

啊,所以我很担心,我只注意到他们参加了'_Bool'抱歉,所以我认为他们正在升级最终使用c99。可悲的是,情况并非如此。顺带一提,你知道MSVC13允许变量声明后的变量声明吗? – dhein

+0

@Zaibis:根据[文档](http://msdn.microsoft.com/en-us/library/ce4b8s02%28v=vs.120%29.aspx),不允许使用'如果有声明,他们必须在任何陈述之前来。“但是我没有编译器在这里测试。 – tinman

+0

刚刚由我自己检查过。 MSVC2013允许在语句已被调用后进行变量声明。 – dhein

相关问题