helloWorld
helloWorld

Reputation: 3049

C compile error: "Variable-sized object may not be initialized"

Why do I receive the error "Variable-sized object may not be initialized" with the following code?

int boardAux[length][length] = {{0}};

Upvotes: 156

Views: 356671

Answers (8)

Ted Lyngmo
Ted Lyngmo

Reputation: 117432

Prior to C23 it wasn't allowed to initialize a variable length array

int boardAux[length][length] = {{0}}; // Error

You therefore had to manually initialize it afterwards. Zero-initializing was often done with a memset.

Since C23, the initialization rules for variable length arrays have changed:

6.7.10 Initialization

  1. The type of the entity to be initialized shall be an array of unknown size or a complete object type. An entity of variable length array type shall not be initialized except by an empty initializer. An array of unknown size shall not be initialized by an empty initializer.

This means that, since C23, you may initialize a VLA, but only with an empty initializer to zero-initialize it.

int boardAux[length][length] = {}; // OK since C23
// memset( boardAux, 0, length*length*sizeof(int) ); // No longer needed

Upvotes: 1

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:

int boardAux[length][length];
memset( boardAux, 0, length*length*sizeof(int) );

Upvotes: 187

Keth
Keth

Reputation: 41

The array is not initialized with the memory specified anf throws an error variable sized array may not be initialised I prefer usual way of initialization,

for (i = 0; i < bins; i++)
        arr[i] = 0;

Upvotes: 3

Oskar Enoksson
Oskar Enoksson

Reputation: 69

Variable length arrays are arrays whose length is not known by the compiler at compile time. In your case length is a variable. I conclude this, because if length was a e.g. preprocessor macro defined as a literal integer your initialization would work. The first C language standard from 1989 did not allow variable length arrays, they were added in 1999. Still the C standard does not allow these to be initialized with an expression like yours (although one could argue that it could or should allow it).

The best way to initialize a variable array is like this:

int boardAux[length][length];
memset( boardAux, 0, sizeof(boardAux) );

memset is a very fast standard library function for initializing memory (to 0 in the above case). sizeof(boardAux) returns the number of bytes occupied by boardAux. sizeof is always available but memset requires #include <string.h>. And yes - sizeof allows a variable sized object as argument.

Note that if you have a normal array (not variable length) and just want to initialize the memory to zero you never need nested brackets, you can initialize it simply like this:

struct whatEver name[13][25] = {0};

Upvotes: 5

Sergey
Sergey

Reputation: 33

The question is already answered but I wanted to point out another solution which is fast and works if length is not meant to be changed at run-time. Use macro #define before main() to define length and in main() your initialization will work:

#define length 10

int main()
{
    int boardAux[length][length] = {{0}};
}

Macros are run before the actual compilation and length will be a compile-time constant (as referred by David Rodríguez in his answer). It will actually substitute length with 10 before compilation.

Upvotes: 0

Krishna Shrestha
Krishna Shrestha

Reputation: 59

After declaring the array

int boardAux[length][length];

the simplest way to assign the initial values as zero is using for loop, even if it may be a bit lengthy

int i, j;
for (i = 0; i<length; i++)
{
    for (j = 0; j<length; j++)
        boardAux[i][j] = 0;
}

Upvotes: 5

Amitesh Ranjan
Amitesh Ranjan

Reputation: 1522

This gives error:

int len;
scanf("%d",&len);
char str[len]="";

This also gives error:

int len=5;
char str[len]="";

But this works fine:

int len=5;
char str[len]; //so the problem lies with assignment not declaration

You need to put value in the following way:

str[0]='a';
str[1]='b'; //like that; and not like str="ab";

Upvotes: 23

AnT stands with Russia
AnT stands with Russia

Reputation: 320621

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.

6.7.8 Initialization

...

3 The type of the entity to be initialized shall be an array of unknown size or an object type that is not a variable length array type.

Upvotes: 35

Related Questions