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