0

I am getting error when i am running this code

int row1=2,col1=2;
int mat1[row1][col1]=
{
 {1,5},
 {4,6}
};

What is wrong with this code??

IDE: CodeBlocks

error: variable-sized object may not be initialized|

asked Nov 21, 2017 at 15:29
9
  • 3
    The error tells it all. Commented Nov 21, 2017 at 15:31
  • 1
    The error says it all. You are trying to make a two-dimensional Variable-Length Array and send it an initializer. Since it is a VLA, it's dimensions are unknown at compile-time, so how can you give it an initializer of fixed dimensions? Commented Nov 21, 2017 at 15:32
  • I'm confused why you need a variable sized array with a fixed size initialiser. That doesn't really make sense. Do you want arrays of different sizes at runtime? Commented Nov 21, 2017 at 15:34
  • I wonder how this gets upvoted? You really can't be any more specific in an error message ... Commented Nov 21, 2017 at 15:37
  • 1
    @Lundin What does the memcpy buy you, especially if the dimensions might actually need to change? Commented Nov 21, 2017 at 15:41

3 Answers 3

5

What you have here is a variable length array. Such an array cannot be initialized. You can only initialize an array if the dimensions are constants (i.e. numeric constants, not variables declared as const):

int mat1[2][2]=
{
 {1,5},
 {4,6}
};
answered Nov 21, 2017 at 15:31
Sign up to request clarification or add additional context in comments.

3 Comments

Quite so, but I suspect that the OP does not appreciate the distinction between integer literals and integer constants expressed via macros. Absent that understanding, this answer would appear to be over-restrictive.
With a fixed initializer, what do macros for the dimensions buy you except the risk for bugs when changing them?
@FelixPalmen Not much, especially with multidimensional arrays, but if you want zeros at the end it can make sense.
2

As per C specs, an array defined like

int mat1[row1][col1]=
{
 {1,5},
 {4,6}
};

is a VLA (Variable Length Array) and cannot be initialized.

Quoting C11, chapter §6.7.6.2/P4,

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

and chapter §6.7.9

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.

You need to use compile time constant expressions as array dimensions to be able to use brace enclosed initializers.

You can use #define MACROs for this, like

#define ROW 2 //compile time constant expression
#define COL 2 //compile time constant expression
int mat1[ROW][COL]=
{
 {1,5},
 {4,6}
};
answered Nov 21, 2017 at 15:31

4 Comments

Baaam. It worked. that means i need to make the variable as final in MACROs.
@Enam It has to be compile time contants, that's all.
But i need to initialize the size of array at run time.now how can i do that so???
@Enam You need to understand, initialization happens in the compile time, you cannot have a VLA, (which determines the size at run time) to be initialized at compile time. Maybe you need pointers and memory allocator functions for that.
0

You are trying to initialize a variable-sized object. You could try assigning the values later somewhere else or simply use numbers instead of variables.

answered Nov 21, 2017 at 15:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.