2

I made this very simple program:

   int a;
   scanf("%i", &a);
   int tab[a];

And there there is the rest of program which works fine when I compile in DevC++. But when I use this:

   int a;
   scanf_s("%i", &a);
   int tab[a];

in Visual Studio 2015 there is a error. I have no idea whats wrong with that.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Finer
  • 95
  • 5
  • 4
    There are no VLAs in Visual Studio. If you really need this, you can use `int *tab = alloca(a * sizeof(*tab)` instead of `int tab[a];`. Don't forget `#include ` – Jabberwocky Nov 01 '17 at 17:00
  • VLA => Variable Length Array – asio_guy Nov 01 '17 at 17:10
  • So how can i declare length of tab from keyboard? I mean by scanf_s – Finer Nov 01 '17 at 17:16
  • @Finer by doing what I suggested in the first comment. – Jabberwocky Nov 01 '17 at 17:16
  • @Finer There's another aspect to consider here as well: This was tagged as a "C" question, but depending on the C standard you're dealing with, what you're compiling successfully in Dev C++ may only be valid by virtue of extension; see [Variable declaration placement in C](https://stackoverflow.com/q/288441/6610379). If it's optional to compile as "C" (and you could just as well work in C++), you'll have some other possibilities. – Phil Brubaker Nov 01 '17 at 17:52
  • What error did you get from VS2015? – jwdonahue Nov 01 '17 at 18:11

1 Answers1

1

Variable Length Arrays (VLA) are part of the C99 Standard, but not part of the C++11/C++14 Standard so they are not implemented by the Visual C++ compiler. As a "C" compiler, Visual C++ is C90 conformant with the portions of the C11 Standard Library that are required by reference in C++11.

Note that the core of this question is already answered here

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81