The variable-length array is an attribute added by C99, that is, the length of the array can be determined at runtime (run time), not at compile time (compile). That is, you do not have to use const int when you define an array, you can use variables, function return values, and so on. For example
1 int Main () {2 int a=5, b=6; 3 int Arr1[a]; 4 int arr2[min (A, b)]; 5 int arr3[rand ()% ]; 6 7 }
The way these arrays are defined is legal.
However, this kind of declarative approach seems to apply only to local variables, and global variables are not. The following usage will cause an error.
int a=; int B[a]; int Main () { 0;}
Allowing the runtime to determine the size of the array can be handy.
However, in the C11 standard, the variable-length array becomes an optional feature that does not require the compiler to implement. I tested the default support for the compilers available at hand.
Compiler |
Whether Variable-length array is supported |
GCC 4.8 |
Support |
Clang 3.4 |
Support |
Visual Studio 2012 |
Not supported |
Visual Studio 2013 |
Not supported |
C,c++ hidden things too much, it is really troublesome. Variable-length Array is a feature that needs to be used sparingly when developing across platforms.
In addition, it is not very appropriate to see many places translated as "variable-length arrays", which are different from the edge-length arrays in other languages.
The
C + + runtime determines the size of the variable-length array