2
int num = atoi(argv[1]);
unsigned long times[num];  

I have this code and I assumed it won't compile because I am trying to allocate the array using a value from a command line argument, which compiler doesn't know at the compile time. But I compiled this code and it worked. Can someone explain what is going on here?? Am I misunderstanding the basic concept of static allocation??

codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
  • 1
    C99 supports [*variable-length arrays*](http://en.wikipedia.org/wiki/Variable-length_array), as seen here. – obataku Sep 05 '12 at 05:41
  • essentially it's up to the compiler to determine a way of allocating the array at runtime, rather than at compile time. – obataku Sep 05 '12 at 05:44

1 Answers1

5

C99 allows to allocate an array with a var. This is called variable length arrays aka VLA

I don't have the C99 in my hand, the section is 6.7.5.2 in C99, and the following links are from the internet.

vla - wikipedia

c99 - wikipedia

be aware that vla is not supported in c++, more information here

Community
  • 1
  • 1
shengy
  • 9,461
  • 4
  • 37
  • 61