2

I read from book: «C Primer Plus» that tells me in C99 standard, it is ok to use grammar such as:

    int b=4;
    char a[b];

But when I put this code into vs2013 it tells me the "expression must have a constant value".

Is the book wrong? Or there is some new feature about vs2013 that I wasn't aware of?

Casey
  • 41,449
  • 7
  • 95
  • 125
azgt
  • 37
  • 1
  • 6
  • 1
    `malloc` is typically used to dynamically size an array. – squiguy Nov 03 '14 at 01:02
  • 8
    Visual Studio is famous for not supporting C properly. – Alex Celeste Nov 03 '14 at 01:02
  • 3
    VS2013 is not a C compiler. – Kerrek SB Nov 03 '14 at 01:03
  • 5
    Microsoft still only supports C89/C90, not C99 and not C11. – Jonathan Leffler Nov 03 '14 at 01:07
  • Microsoft currently supports a subset of the C99 standard in Visual Studio 2013 and variable length arrays is one of the missing pieces. You may use C Preprocessor defines if this is a compile time known size or a memory allocation (`malloc()`, `alloc()`, etc.) if run time sizing is required. See also this [why is alloca not considered good practice](http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice) – Richard Chambers Nov 03 '14 at 02:15
  • @Leushenko Can you recommend me an IDE for windows? – azgt Nov 03 '14 at 02:33
  • Also I understand that variable length array allocation can fail if there is insufficient stack space similar to what can happen with the `alloc()` function. – Richard Chambers Nov 03 '14 at 02:35
  • @RichardChambers the C standard does not cover stack overflow at all (whether it be via VLA or otherwise) – M.M Nov 03 '14 at 03:19
  • @ArcherJohn personally I prefer lightweight text editors to big IDEs. Try something like Jedit or Geany or Notepad++ if you don't like Eclipse. Get MinGW separately. – Alex Celeste Nov 03 '14 at 04:45
  • @MattMcNabb, I did not mention anything about what the C standard says about stack overflow. What I did say is that a VLA allocation with a function, which will go onto the stack, may cause a stack overflow according to what I have read such as this stack overflow question [what's the point of VLA anyway](http://stackoverflow.com/questions/22530363/whats-the-point-of-vla-anyway) – Richard Chambers Nov 03 '14 at 05:37

2 Answers2

7

This is called Variable Length Arrays, and I guess that your VS2013 compiler don't support them.

I would advise you to switch to another compiler. Recent versions of GCC or of Clang/LLVM support VLAs, and they support C99 and even most of C11.

Actually I would even suggest to switch your development efforts and system to Linux. Development tools are then usually free software, as the whole system.

Otherwise, use a pointer, e.g. obtained from malloc(3) (or calloc); but then, you should test the allocation against failure and later call free. Avoid memory leaks (use some tool like valgrind if available). Maybe your compiler supports the non-standard but widely available alloca(3). Consider perhaps also using program-wide a garbage collector like Boehm's convervative GC: you'll then use GC_malloc instead of malloc but you won't need to call GC_free !

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I was trying to get used to the eclipse in linux, but just can't get used to it. And there are some people recommend me vim editor. That is even worse, I don't even know how to use that – azgt Nov 03 '14 at 02:16
  • You could just use `emacs` or `gedit` or `geany` (or many other editors) under Linux. – Basile Starynkevitch Nov 03 '14 at 06:03
  • I am just a beginner... I have no clue how to use those plain editors... I think I am better to stay with IDEs – azgt Nov 04 '14 at 13:04
-1

VS2013 don't support variable length declaration.

You can't enter a non-constant value between the brackets when you declare your array:

char a[b];

Since you're getting size from the user, the compiler can't tell ahead of time how much memory it needs for char array a. The easiest thing to do here (especially for an exercise) is to just choose a relatively large value and make that the constant allocation, like:

char a[1024];

And then if you want to be careful (and you should) you can check if (b > 1024) and print an error if the user wants b that's beyond the pre-allocated bounds.

If you want to get fancy, you can define char a[] with no pre-set size, like char *a; and then you allocate it later with malloc:

a = (char *)malloc(sizeof(char) * b);

Then you must also free char a[] later, when you're done with it:

free(a);

Hope it helps!

Blackhat002
  • 598
  • 4
  • 12