0

I'm currently learning C language from youtube and this is one of the code about the 2D arrays:

#include<stdio.h>

int main()
{

    int const columns = 3;
    int const rows = 2;
    int grades[rows][columns] = {
        {12, 23, 45},
        {64, 78, 89}
    };
    
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            printf("%d ", grades[i][j]);
        }
    }
    
    return 0;
}

But when I try to run it, it shows the error "error: variable-sized object may not be initialized"

int grades[rows][columns] = {

      |     ^~~
tempCodeRunnerFile.c:8:10: warning: excess elements in array initializer

    8 |         {12, 23, 45},
      |          ^~
tempCodeRunnerFile.c:8:10: note: (near initialization for 'grades[0]')

So on and so forth. I can't figure it out and it keeps struggling me.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Camille
  • 1
  • 1
  • It is as the error says: this is not allowed by the C standard. If you are using Microsoft Visual C, you can't use VLAs at all. – Weather Vane Nov 19 '20 at 15:16

2 Answers2

2

the code is C not C++ so this:

int const columns = 3;
int const rows = 2;
int grades[rows][columns] = {
    {12, 23, 45},
    {64, 78, 89}
};

needs to be changed to:

#define COLUMNS 3
#define ROWS 2
int grades[ ROWS ][ COLUMNS ] = {
    {12, 23, 45},
    {64, 78, 89}
};
user3629249
  • 16,402
  • 1
  • 16
  • 17
0

In C the size of a not variable length array shall be an integer constant expression that is defined the following way (The C Standard, 6.6 Constant expressions)

6 An integer constant expression117) shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.

and (The C Standard, 6.7.6.2 Array declarators)

  1. ...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 at last (The C Standard, 6.7.9 Initialization)

3 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.

So variables even with the qualifier const may not be used in an integer constant expression.

Thus in this declaration

int grades[rows][columns] = {
    {12, 23, 45},
    {64, 78, 89}
}; 

there is declared a variable length array that according to the last quote may not be initialized.

You have two approaches to define the sizes of the array. Either use defined preprocessor constants like

#define columns 3
#define rows 2

Or use enumeration constants like

enum { columns = 3, rows = 2 };
int grades[rows][columns] = {
    {12, 23, 45},
    {64, 78, 89}
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335