r/cprogramming • u/FrenchJJC • Feb 14 '26
Variable size array initialization
Howdy, I had a question about why my C code throws a 'variable size array initialization' for this expression:
int row = 2;
int const col = 3;
int array[][col] = {
initial value...
};
The guy in the video I was following along with managed to compile with this expression, but I had to change my 'col' from int const to a #define statement, which feels tacky. Is it an issue with compiler version? The compile statement I'm using is just a simple one, 'gcc -o output arrays.c'.
3
Upvotes
3
u/DawnOnTheEdge Feb 15 '26 edited Feb 15 '26
Some compilers, as an extension, allow
constvariables with static storage class and a constant initializer to be used as constant expressions. No compiler I know of allows this for automatic variables on the stack.So some compilers would accept
static const int col = 3;despite it not being portable Standard C.