Dynamic array allocation on stack in C
I just did a experiment yesterday, and find something confusing: #include <stdio.h> int main() { int j; scanf("%d",&j); const int i = j; int arr[i]; return 0; } The number j is read from keyboard and it’s used to allocate the array arr on the stack. The compiler does not even know the size of the array at compile time (initializes j to 0?), but there is no compilation error. How is it possible? Variable length arrays were added to C99. It's described in the C99 rationale: 6.7.5.2 Array declarators C99 adds a new array type called a variable length array type. The inability to declare arrays