This is wrong since a variable sized object may not be initialized
int size = 4;
int array[size] = {1};
size
is a variable, but doesn't the compiler know its value when it creates array
(Isn't size
assigned an initial value of 4 at compile-time?)? Let size
change after that, why would it be an issue? I mean, these are consecutive instructions, what could possibly alter the value of size
before the array is declared?
Second question: Why is this not allowed:
const int size = 4;
int array[size] = {1};
I am declaring size
as a const. I know that const != read-only, and that declaring size
as a macro is the correct way to go about it. But if I promise the compiler using const that I wont change the value of size
, why is it not allowed?