C语言 C 编译错误:“可能无法初始化可变大小的对象”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3082914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:42:24  来源:igfitidea点击:

C compile error: "Variable-sized object may not be initialized"

ccompiler-errorsinitializer-listvariable-length-array

提问by helloWorld

Why do I receive the error "Variable-sized object may not be initialized" with the following code?

为什么我收到以下代码的错误“可变大小的对象可能无法初始化”?

int boardAux[length][length] = {{0}};

回答by David Rodríguez - dribeas

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 lengthis not a compile time constant).

我假设您使用的是 C99 编译器(支持动态大小的数组)。您的代码中的问题是,当编译器看到您的变量声明时,它无法知道数组中有多少个元素(我也在这里假设,来自length不是编译时常量的编译器错误)。

You must manually initialize that array:

您必须手动初始化该数组:

int boardAux[length][length];
memset( boardAux, 0, length*length*sizeof(int) );

回答by AnT

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

...

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.

6.7.8 初始化

...

3 待初始化实体的类型应为大小未知的数组或非变长数组类型的对象类型。

回答by Amitesh Ranjan

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";

回答by Krishna Shrestha

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;
}

回答by Sergey

The question is already answered but I wanted to point out another solution which is fast and works if length is not meant to be changed at run-time. Use macro #define before main() to define length and in main() your initialization will work:

问题已经回答,但我想指出另一种解决方案,如果不打算在运行时更改长度,该解决方案既快速又有效。在 main() 之前使用宏 #define 定义长度,在 main() 中,您的初始化将起作用:

#define length 10

int main()
{
    int boardAux[length][length] = {{0}};
}

Macros are run before the actual compilation and length will be a compile-time constant (as referred by David Rodríguez in his answer). It will actually substitute length with 10 before compilation.

宏在实际编译之前运行,长度将是一个编译时常量(如 David Rodríguez 在他的回答中提到的)。它实际上会在编译前用 10 替换长度。

回答by Codetheft

int size=5;
int ar[size ]={O};

/* This  operation gives an error -  
variable sized array may not be 
initialised.  Then just try this. 
*/
int size=5,i;
int ar[size];
for(i=0;i<size;i++)
{
    ar[i]=0;
}

回答by Azizou

Simply declare length to be a cons, if it is not then you should be allocating memory dynamically

只需将长度声明为缺点,如果不是,则您应该动态分配内存

回答by Azizou

For C++ separate declaration and initialization like this..

对于 C++ 这样的单独声明和初始化..

int a[n][m] ;
a[n][m]= {0};

回答by Pavel Radzivilovsky

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
  • call malloc(size) to allocate the memory
  • create an accessor: int* access(ptr,x,y,rowSize) { return ptr + y*rowSize + x; }
  • 计算您需要的内存的大小 (n msizeof(element))
  • 调用 malloc(size) 分配内存
  • 创建一个访问器: int* access(ptr,x,y,rowSize) { return ptr + y*rowSize + x; }

Use *access(boardAux, x, y, size) = 42 to interact with the matrix.

使用 *access(boardAux, x, y, size) = 42 与矩阵交互。