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??

2
  • 1
    C99 supports variable-length arrays, as seen here. Commented Sep 5, 2012 at 5:41
  • essentially it's up to the compiler to determine a way of allocating the array at runtime, rather than at compile time. Commented Sep 5, 2012 at 5:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.