21

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};

?

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 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 – router Dec 15 '22 at 05:39

3 Answers3

27

You'll have to use memset:

memset(board, 0, sizeof board);
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
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.

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 2
    you mean to say `int board[n][n];` `board[n][n] = {0};`. right? – haccks Jun 27 '13 at 00:05
  • 3
    @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. – AnT stands with Russia Jun 27 '13 at 00:37
  • 1
    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 Jun 27 '13 at 00:46
  • 8
    @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. – AnT stands with Russia Jun 27 '13 at 01:59
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".

The Matt
  • 1,423
  • 1
  • 12
  • 22
PG1
  • 1,220
  • 2
  • 12
  • 27
  • "*... memset() should be usesd only when you want to set the array to "0".*" Hu! Why this? – alk Dec 24 '13 at 16:42
  • 1
    @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". – PG1 Dec 26 '13 at 07:49
  • 2
    @ParagGangil; *memset() should be usesd only when you want to set the array to "0"*: [Not true](http://stackoverflow.com/q/24207698/2455888). – haccks Mar 10 '15 at 19:02