23

On initializing a Variable length array compiler gives an error message:

[Error] variable-sized object may not be initialized  

Code snippet:

int n; 
printf("Enter size of magic square: ");
scanf("%d",&n);

int board[n][n] = {0};

How should Variable Length arrays be initialized? And why it's all elements are not initialized to 0 in the way give below;

   int board[n][n];
   board[n][n] = {0};

?

1
  • If you are using cpp then use fill(&board[0][0], &board[n-1][n-1]+1, 0) to initialize all elements with 0 or any value you choose Commented Dec 15, 2022 at 5:39

3 Answers 3

28

You'll have to use memset:

memset(board, 0, sizeof board);
Sign up to request clarification or add additional context in comments.

5 Comments

What does that mean?
@haccks you just put that below your line int board[n][n];. And make sure you #include <string.h>
#include <string.h> for C.
You wanted to initialize it to 0, right?
memset is clean you can also shift left the size of the array… In this case *board <<= n * n;
22

VLAs cannot be initialized by any form of initialization syntax. You have to assign the initial values to your array elements after the declaration in whichever way you prefer.

C11: 6.7.9 Initialization (p2 and p3):

No initializer shall attempt to provide a value for an object not contained within the entity being initialized.

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

4 Comments

you mean to say int board[n][n]; board[n][n] = {0};. right?
@haccks: No. Arrays are not assignable. This is why I said "assign the initial values to your array elements". Barring such raw-memory operations like memset or memcpy, it has to be done in element by element fashion. So, I meant to say for (i = 0; i < n; ++i) ... and so on.
I read that array can be initialized as array[m][n] = {0}, this will initialize its first element to 0 and remaining elements get initialized to 0 itself.
@haccks: "Initialized" means that = { 0 } can be specified as an initializer in a declaration. What you have in your first comment is completely incorrect. And even in a declaration it works with non-VLA arrays only. For example, you can do int array[10][10] = { 0 }. Yet this question is specifically about VLA arrays. VLA arrays do not accept initializers. You cannot do int array[m][n] = { 0 } when m and n are not constants.
0

1.You can simply initialize the array as follows-

int n; 
printf("Enter size of magic square: ");
scanf("%d",&n);

int board[n][n];
for(int i=0; i<n; i++)
   for(int j=0; j<n; j++)
   {
      board[i][j] = 0;
   }
}

2. memset() should only be used when you want to set the array to "0".

3 Comments

"... memset() should be usesd only when you want to set the array to "0"." Hu! Why this?
@alk : The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. Whereas, size of integer is generally 4 bytes so we cant set the integer to our desired number but "0" is a special case since with "0", memset() sets all bytes to "0".
@ParagGangil; memset() should be usesd only when you want to set the array to "0": Not true.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.