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|

  • 3
    The error tells it all. –  Nov 21 '17 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? – Christian Gibbons Nov 21 '17 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? – Philip Couling Nov 21 '17 at 15:34
  • I wonder how **this** gets upvoted? You really can't be any more specific in an error message ... –  Nov 21 '17 at 15:37
  • Simple fix: `const int init_mat [2][2] = { {1,5}, {4,6} }; memcpy(mat1, init_mat, sizeof init_mat);` – Lundin Nov 21 '17 at 15:38
  • 1
    @Lundin What does the `memcpy` buy you, *especially* if the dimensions might actually need to change? – Daniel H Nov 21 '17 at 15:41
  • @DanielH It buys me the fastest possible way to initialize a VLA? Please note that `const int init_mat [2][2]` could be a local scope variable and could be changed accordingly. Perhaps I should have written `{ const int init_mat[MAX_X][MAX_Y] = { parameters }; ...`. – Lundin Nov 21 '17 at 15:44
  • what is row2 and col2, its an unused variable? – danglingpointer Nov 21 '17 at 15:44
  • @Lundin If the result is a VLA where the lengths are actually runtime variable (unlike in this example), then `memcpy` will put the numbers in the wrong cells. If not, then it's better to just make it a fixed-length array. – Daniel H Nov 21 '17 at 15:51

3 Answers3

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}
};
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 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. – John Bollinger Nov 21 '17 at 15:36
  • With a fixed initializer, what do macros for the dimensions buy you except the risk for bugs when changing them? –  Nov 21 '17 at 15:38
  • @FelixPalmen Not much, especially with multidimensional arrays, but if you want zeros at the end it can make sense. – Daniel H Nov 21 '17 at 15:39
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}
};
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Baaam. It worked. that means i need to make the variable as final in MACROs. –  Nov 21 '17 at 15:41
  • @Enam It has to be compile time contants, that's all. – Sourav Ghosh Nov 21 '17 at 15:52
  • But i need to initialize the size of array at run time.now how can i do that so??? –  Nov 21 '17 at 15:56
  • @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. – Sourav Ghosh Nov 21 '17 at 15:57
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.