在MS Visual C ++中启用VLA(可变长度数组)? [英] Enabling VLAs (variable length arrays) in MS Visual C++?

查看:197
本文介绍了在MS Visual C ++中启用VLA(可变长度数组)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MS Visual C ++中启用VLA,C99中定义的可变长度数组,或者根本无法使用?

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:期望的常量表达式

error C2057: expected constant expression

错误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(可变长度数组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆