6

I'm seeing some code like this:

int foo()
{
  int sz = call_other_func();
  char array[sz];

  /* whatever */
}

I'm baffled at how this would work and even compile with gcc. The size of the array is supposed to be static and determined at compile time, no?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
lang2
  • 11,433
  • 18
  • 83
  • 133
  • 1
    As for how would it work -- why *wouldn't* it work? – Kerrek SB Nov 20 '13 at 12:47
  • possible duplicate of [How is size of variable length array computed at runtime in C99?](http://stackoverflow.com/questions/13908421/how-is-size-of-variable-length-array-computed-at-runtime-in-c99) – M.M Nov 03 '14 at 06:32
  • possible duplicate of [Declare an array with a variable](http://stackoverflow.com/questions/19131712/declare-an-array-with-a-variable) – phuclv Nov 18 '14 at 08:22

3 Answers3

4

This type of arrays are called variable length arrays (you would also like to raed: Arrays of Variable Length - GCC) and are allowed in C99 only. By using VLAs, the size of the array can be determine at runtime.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Although people are upvoting this answer, don't know why reputation is not added to my profile? – haccks Nov 20 '13 at 13:33
  • 1
    Look like you are [rep capped](http://meta.stackexchange.com/questions/32160/what-is-the-daily-reputation-cap-and-how-can-i-hit-it), congrats. – Shafik Yaghmour Nov 20 '13 at 13:34
  • @ShafikYaghmour; Thanks. But many of the users are getting reputation around 275 or even more in a single day(I saw this), how? – haccks Nov 20 '13 at 13:43
  • 1
    I changed the link I provided to a better answer so you might have seen the old one, the answer to the meta question says `You can still earn rep by winning a bounty, accepting an answer, or having one of your answers accepted, and downvotes will still count against you.` If you don't spend much time on meta at this point you probably should. – Shafik Yaghmour Nov 20 '13 at 13:45
  • @ShafikYaghmour; I have not get any of the `epic` or legendary badge! I hit +200 many times but that reputation never goes beyond 231. (I see you also got 261 rep once). – haccks Nov 20 '13 at 13:53
  • You have to hit your rep cap 50 times for epic and 200 times for legendary, that will usually take a while. – Shafik Yaghmour Nov 20 '13 at 13:54
  • @ShafikYaghmour; Until then I have to wait for getting reputation more than `231`. right? – haccks Nov 20 '13 at 13:56
4

"In programming, a variable-length array (or VLA) is an array data structure of automatic storage duration whose length is determined at run time (instead of at compile time)." (Wikipedia)

They're supported in C99 (and subsequently in C11).

Read more about how it works: The New C: Why Variable-length Arrays?

Eutherpy
  • 4,471
  • 7
  • 40
  • 64
3

This is valid C99 feature called variable length arrays(VLA), if you compile with gcc -std=c90 -pedantic you will receive the following warning:

warning: ISO C90 forbids variable length array ‘array’ [-Wvla]

using -std=c99 -pedantic will not produce a warning, although both gcc and clang support VLA outside of C99 mode and also in C++ which does not allow VLA as an extension.

We can see from the C99 draft standard section 6.7.5.2 Array declarators paragraph 4 says (emphasis mine):

If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations with function prototype scope;124) such arrays are nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Note that Visual Studio does not support VLA even though they now support C99

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740