Why do I receive the error "Variable-sized object may not be initialized" with the following code?
int boardAux[length][length] = {{0}};
Why do I receive the error "Variable-sized object may not be initialized" with the following code?
int boardAux[length][length] = {{0}};
After declaring the array
the simplest way to assign the initial values as zero is using for loop, even if it may be a bit lengthy
Simply declare length to be a cons, if it is not then you should be allocating memory dynamically
This gives error:
This also gives error:
But this works fine:
You need to put value in the following way:
For C++ separate declaration and initialization like this..
You receive this error because in C language you are not allowed to use initializers with variable length arrays. The error message you are getting basically says it all.
I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that
length
is not a compile time constant).You must manually initialize that array: