0

The following code wont compile. It give me an error that 'a constant size was expected'. Is there any way or a compiler flag or a preprocessor directive to add, to cause enable support for C99 features such as the VLAs.

#include "Window.h"
#include <stdio.h>

int main( int argc, char** argv)
{
   const int size = 1024 ;
   char filename[size]; 
}
Kobby Owen
  • 85
  • 1
  • 8
  • 4
    Does this answer your question? [Enabling VLAs (variable length arrays) in MS Visual C++?](https://stackoverflow.com/questions/5246900/enabling-vlas-variable-length-arrays-in-ms-visual-c) – dragosht Jan 16 '20 at 10:13
  • Probably depends on which version of the compiler you have. They made some half-hearted attempt to partially support C99 couple of years ago. Or if your question is what's the best alternative to VC, then pretty much any random C compiler made this millennium is better. – Lundin Jan 16 '20 at 10:17
  • What kind of alternative are you looking for? MinGW? – jxh Jan 16 '20 at 10:18
  • Note that VLAs became optional in C11 (implementation must define the macro `__STDC_NO_VLA__` to be `1` in that case). – pmg Jan 16 '20 at 10:18
  • 1
    Use a macro instead of a variable: `#define SIZE 1024` ... `filename[SIZE];` – Burdui Jan 16 '20 at 11:32
  • AFAIK, you could keep the IDE, but change the compiler, see e.g. https://devblogs.microsoft.com/cppblog/clang-llvm-support-in-visual-studio/, https://devblogs.microsoft.com/cppblog/use-any-c-compiler-with-visual-studio/ or https://devblogs.microsoft.com/cppblog/using-mingw-and-cygwin-with-visual-cpp-and-open-folder/ – Bob__ Jan 16 '20 at 14:21

1 Answers1

0

As already outlined in comments, your (obvious) options with C+MSVC would be:

  1. A macro (you can #undef it when no longer needed)
  2. alloca (shown here), which allocates on stack and does not require explicit deallocation; you may wrap it in a macro for a convenient "create X-sized array of Y" pseudo-function

As it was answered on now-dead Microsoft Connect, MSVC does not support C99 VLAs as such, which likely didn't change since then.

YellowAfterlife
  • 2,967
  • 1
  • 16
  • 24