Why do I receive the error "Variable-sized object may not be initialized" with the following code?
为什么我得到的错误“可变大小的对象可能不会被初始化”?
int boardAux[length][length] = {{0}};
8 个解决方案
#1
73
I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length
is not a compile time constant).
我假设您使用的是C99编译器(支持动态大小的数组)。您的代码中的问题是,在编译器看到您的变量声明时,它无法知道数组中有多少个元素(我也假设在这里,从编译器错误中,长度不是编译时常量)。
You must manually initialize that array:
您必须手动初始化该数组:
int boardAux[length][length];
memset( boardAux, 0, length*length*sizeof(int) );
#2
17
You receive this error because in C language you are not allowed to use initializers with variable length arrays. The error message you are getting basically says it all.
您会收到这个错误,因为在C语言中,您不允许使用具有可变长度数组的初始化程序。你所得到的错误信息基本上说明了一切。
6.7.8 Initialization
6.7.8初始化
...
…
3 The type of the entity to be initialized shall be an array of unknown size or an object type that is not a variable length array type.
要初始化的实体的类型应该是一个未知大小的数组或不是可变长度数组类型的对象类型。
#3
6
This gives error:
这给了错误:
int len;
scanf("%d",&len);
char str[len]="";
This also gives error:
这也给了错误:
int len=5;
char str[len]="";
But this works fine:
但这工作正常:
int len=5;
char str[len]; //so the problem lies with assignment not declaration
You need to put value in the following way:
你需要用以下方法来表达你的价值:
str[0]='a';
str[1]='b'; //like that; and not like str="ab";
#4
1
After declaring the array
在声明数组
int boardAux[length][length];
the simplest way to assign the initial values as zero is using for loop, even if it may be a bit lengthy
将初始值赋值为零的最简单方法是使用for循环,即使它可能有点长。
int i, j;
for (i = 0; i<length; i++)
{
for (j = 0; j<length; j++)
boardAux[i][j] = 0;
}
#5
1
For C++ separate declaration and initialization like this..
对于c++独立的声明和初始化。
int a[n][m] ;
a[n][m]= {0};
#6
0
Simply declare length to be a cons, if it is not then you should be allocating memory dynamically
简单地声明长度为一个缺点,如果不是,那么您应该动态地分配内存。
#7
-1
Another way for C++:
c++的另一种方式:
const int n = 5;
const int m = 4;
int a[n][m] = {0};
#8
-5
You cannot do it. C compiler cannot do such a complex thing on stack.
你不能这样做。C编译器不能在堆栈上做这么复杂的事情。
You have to use heap and dynamic allocation.
您必须使用堆和动态分配。
What you really need to do:
你真正需要做的是:
- compute size (nmsizeof(element)) of the memory you need
- 计算所需内存的大小(nmsizeof(元素))。
- call malloc(size) to allocate the memory
- 调用malloc(大小)来分配内存。
- create an accessor: int* access(ptr,x,y,rowSize) { return ptr + y*rowSize + x; }
- 创建一个accessor: int* access(ptr,x,y,rowSize){返回ptr + y*rowSize + x;}
Use *access(boardAux, x, y, size) = 42 to interact with the matrix.
使用*access(boardAux, x, y, size) = 42来与矩阵交互。
#1
73
I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length
is not a compile time constant).
我假设您使用的是C99编译器(支持动态大小的数组)。您的代码中的问题是,在编译器看到您的变量声明时,它无法知道数组中有多少个元素(我也假设在这里,从编译器错误中,长度不是编译时常量)。
You must manually initialize that array:
您必须手动初始化该数组:
int boardAux[length][length];
memset( boardAux, 0, length*length*sizeof(int) );
#2
17
You receive this error because in C language you are not allowed to use initializers with variable length arrays. The error message you are getting basically says it all.
您会收到这个错误,因为在C语言中,您不允许使用具有可变长度数组的初始化程序。你所得到的错误信息基本上说明了一切。
6.7.8 Initialization
6.7.8初始化
...
…
3 The type of the entity to be initialized shall be an array of unknown size or an object type that is not a variable length array type.
要初始化的实体的类型应该是一个未知大小的数组或不是可变长度数组类型的对象类型。
#3
6
This gives error:
这给了错误:
int len;
scanf("%d",&len);
char str[len]="";
This also gives error:
这也给了错误:
int len=5;
char str[len]="";
But this works fine:
但这工作正常:
int len=5;
char str[len]; //so the problem lies with assignment not declaration
You need to put value in the following way:
你需要用以下方法来表达你的价值:
str[0]='a';
str[1]='b'; //like that; and not like str="ab";
#4
1
After declaring the array
在声明数组
int boardAux[length][length];
the simplest way to assign the initial values as zero is using for loop, even if it may be a bit lengthy
将初始值赋值为零的最简单方法是使用for循环,即使它可能有点长。
int i, j;
for (i = 0; i<length; i++)
{
for (j = 0; j<length; j++)
boardAux[i][j] = 0;
}
#5
1
For C++ separate declaration and initialization like this..
对于c++独立的声明和初始化。
int a[n][m] ;
a[n][m]= {0};
#6
0
Simply declare length to be a cons, if it is not then you should be allocating memory dynamically
简单地声明长度为一个缺点,如果不是,那么您应该动态地分配内存。
#7
-1
Another way for C++:
c++的另一种方式:
const int n = 5;
const int m = 4;
int a[n][m] = {0};
#8
-5
You cannot do it. C compiler cannot do such a complex thing on stack.
你不能这样做。C编译器不能在堆栈上做这么复杂的事情。
You have to use heap and dynamic allocation.
您必须使用堆和动态分配。
What you really need to do:
你真正需要做的是:
- compute size (nmsizeof(element)) of the memory you need
- 计算所需内存的大小(nmsizeof(元素))。
- call malloc(size) to allocate the memory
- 调用malloc(大小)来分配内存。
- create an accessor: int* access(ptr,x,y,rowSize) { return ptr + y*rowSize + x; }
- 创建一个accessor: int* access(ptr,x,y,rowSize){返回ptr + y*rowSize + x;}
Use *access(boardAux, x, y, size) = 42 to interact with the matrix.
使用*access(boardAux, x, y, size) = 42来与矩阵交互。