The problem is that const
in C does not lead to a true constant.
When you write const int i = 5
what you have is a read-only variable, not a constant. In C99, an array whose size is i
is a variable length array (VLA). VLAs are only available for stacked variables, therefore you see a compilation error.
If you need an array with a global scope, you must switch to a macro.
#define ARRAY_SIZE 5 int arry[ARRAY_SIZE];
This is true because 5
is a literal, which is a true constant.
In fact, even for an automatic storage array (i.e. a distributed local table), you should avoid VLAs, as they incur overhead at runtime.
source share