问题描述
如何启用 VLA、C99 中定义的可变长度数组、MS Visual C++ 或根本不可能的使用?
How can I enable the use of VLAs, variable length arrays as defined in C99, in MS Visual C++ or that is not possible at all?
是的,我知道 C++ 标准基于 C89,并且 VLA 在 C89 标准中不可用,因此在 C++ 中不可用,但 MSVC++ 也应该是 C 编译器,一种可以打开的行为使用/TC 编译器参数(Compile as C Code (/TC)
).但是这样做似乎并没有启用 VLA,并且编译过程在构建为 C++ 时失败并出现相同的错误(Compile as C++ Code (/TP)
).也许 MSVC++ C 编译器仅符合 C89,或者我遗漏了一些东西(一些特殊的构造或编译指示/定义)?
Yes I know that the C++ standard is based on C89 and that VLAs are not available in C89 standard and thus aren't available in C++, but MSVC++ is supposed to be a C compiler also, a behavior that can be switched on using the /TC compiler parameter (Compile as C Code (/TC)
). But doing so does not seem to enable VLAs and the compiling process fails with the same errors when building as C++ (Compile as C++ Code (/TP)
). Maybe MSVC++ C compiler is C89 compliant only or I am missing something (some special construct or pragma/define)?
代码示例:
#include <stdlib.h>
int main(int argc, char **argv)
{
char pc[argc+5];
/* do something useful with pc */
return EXIT_SUCCESS;
}
编译错误:
错误 C2057:预期的常量表达式
错误 C2466:无法分配大小为 0 的常量
error C2466: cannot allocate an array of constant size 0
错误 C2133:'pc':未知大小
error C2133: 'pc' : unknown size
推荐答案
MSVC 不是 C99 编译器,不支持变长数组.
MSVC is not a C99 compiler, and does not support variable length arrays.
在 https://docs.microsoft.com/en-us/cpp/c-language/ansi-conformance MSVC 被记录为符合 C90.
At https://docs.microsoft.com/en-us/cpp/c-language/ansi-conformance MSVC is documented as conforming to C90.
这篇关于在 MS Visual C++ 中启用 VLA(可变长度数组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!