Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

Enabling VLAs (variable length arrays) in 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?

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)?

Code sample:

#include <stdlib.h>

int main(int argc, char **argv)
{
  char pc[argc+5];

  /* do something useful with pc */

  return EXIT_SUCCESS;
}

Compile errors:

error C2057: expected constant expression

error C2466: cannot allocate an array of constant size 0

error C2133: 'pc' : unknown size

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

MSVC is not a C99 compiler, and does not support variable length arrays.

At https://docs.microsoft.com/en-us/cpp/c-language/ansi-conformance MSVC is documented as conforming to C90.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...