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
IDE:CodeBlocks
error: variable-sized object may not be initialized|
错误:可能无法初始化可变大小的对象
3 个解决方案
#1
2
As per C specs, an array defined like
根据C规范,数组定义为
int mat1[row1][col1]=
{
{1,5},
{4,6}
};
is a VLA (Variable Length Array) and cannot be initialized.
是VLA(可变长度数组),无法初始化。
Quoting C11
, chapter §6.7.6.2/P4,
引用C11,章节§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.
[...]如果size是一个整型常量表达式,并且元素类型具有已知的常量大小,则数组类型不是可变长度数组类型;否则,数组类型是可变长度数组类型。
and chapter §6.7.9
和章节§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 MACROs,比如
#define ROW 2 //compile time constant expression
#define COL 2 //compile time constant expression
int mat1[ROW][COL]=
{
{1,5},
{4,6}
};
#2
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
):
你在这里有一个可变长度数组。这样的数组不能初始化。如果维度是常量(即数字常量,而不是声明为const的变量),则只能初始化数组:
int mat1[2][2]=
{
{1,5},
{4,6}
};
#3
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.
您正在尝试初始化可变大小的对象。您可以尝试稍后在其他地方分配值,或者只使用数字而不是变量。
#1
2
As per C specs, an array defined like
根据C规范,数组定义为
int mat1[row1][col1]=
{
{1,5},
{4,6}
};
is a VLA (Variable Length Array) and cannot be initialized.
是VLA(可变长度数组),无法初始化。
Quoting C11
, chapter §6.7.6.2/P4,
引用C11,章节§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.
[...]如果size是一个整型常量表达式,并且元素类型具有已知的常量大小,则数组类型不是可变长度数组类型;否则,数组类型是可变长度数组类型。
and chapter §6.7.9
和章节§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 MACROs,比如
#define ROW 2 //compile time constant expression
#define COL 2 //compile time constant expression
int mat1[ROW][COL]=
{
{1,5},
{4,6}
};
#2
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
):
你在这里有一个可变长度数组。这样的数组不能初始化。如果维度是常量(即数字常量,而不是声明为const的变量),则只能初始化数组:
int mat1[2][2]=
{
{1,5},
{4,6}
};
#3
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.
您正在尝试初始化可变大小的对象。您可以尝试稍后在其他地方分配值,或者只使用数字而不是变量。