C 编译错误:“可变大小的对象可能未初始化”
为什么我使用以下代码收到错误“可变大小的对象可能未初始化”?
int boardAux[length][length] = {{0}};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为什么我使用以下代码收到错误“可变大小的对象可能未初始化”?
int boardAux[length][length] = {{0}};
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
我假设您使用的是 C99 编译器(支持动态大小的数组)。您代码中的问题是,当编译器看到您的变量声明时,它无法知道数组中有多少个元素(我在这里还假设,从编译器错误中
length
不是编译时间常数)。您必须手动初始化该数组:
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).You must manually initialize that array:
您收到此错误是因为在 C 语言中不允许使用具有可变长度数组的初始值设定项。您收到的错误消息基本上说明了一切。
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.
这会给出错误:
这也会给出错误:
但这工作正常:
您需要按以下方式输入值:
This gives error:
This also gives error:
But this works fine:
You need to put value in the following way:
声明数组后,
将初始值分配为零的最简单方法是使用 for 循环,即使它可能有点长
After declaring the array
the simplest way to assign the initial values as zero is using for loop, even if it may be a bit lengthy
变长数组是编译器在编译时不知道其长度的数组。在您的情况下
length
是一个变量。我得出这样的结论,因为如果length
是一个定义为文字整数的预处理器宏,那么您的初始化将起作用。 1989 年的第一个 C 语言标准不允许可变长度数组,它们是在 1999 年添加的。 C 标准仍然不允许使用像您这样的表达式来初始化它们(尽管有人可能会认为它可以或应该允许)。初始化变量数组的最佳方法如下:
memset 是一个非常快速的标准库函数,用于初始化内存(在上述情况下为 0)。
sizeof(boardAux)
返回boardAux
占用的字节数。sizeof
始终可用,但memset
需要#include
。是的 - sizeof 允许可变大小的对象作为参数。请注意,如果您有一个普通数组(不是可变长度)并且只想将内存初始化为零,则您永远不需要嵌套括号,您可以像这样简单地初始化它:
Variable length arrays are arrays whose length is not known by the compiler at compile time. In your case
length
is a variable. I conclude this, because iflength
was a e.g. preprocessor macro defined as a literal integer your initialization would work. The first C language standard from 1989 did not allow variable length arrays, they were added in 1999. Still the C standard does not allow these to be initialized with an expression like yours (although one could argue that it could or should allow it).The best way to initialize a variable array is like this:
memset
is a very fast standard library function for initializing memory (to 0 in the above case).sizeof(boardAux)
returns the number of bytes occupied byboardAux
.sizeof
is always available butmemset
requires#include <string.h>
. And yes -sizeof
allows a variable sized object as argument.Note that if you have a normal array (not variable length) and just want to initialize the memory to zero you never need nested brackets, you can initialize it simply like this:
数组未使用指定的内存进行初始化,anf 会引发错误
可变大小的数组可能无法初始化
我更喜欢通常的初始化方式,
The array is not initialized with the memory specified anf throws an error
variable sized array may not be initialised
I prefer usual way of initialization,
在 C23 之前,不允许初始化可变长度数组,
因此您必须随后手动初始化它。零初始化通常使用
memset
来完成。从 C23 开始,变长数组的初始化规则发生了变化:
6.7.10 初始化
这意味着,从 C23 开始,您可以初始化 VLA,但只能使用空初始化程序对其进行零初始化。
Prior to C23 it wasn't allowed to initialize a variable length array
You therefore had to manually initialize it afterwards. Zero-initializing was often done with a
memset
.Since C23, the initialization rules for variable length arrays have changed:
6.7.10 Initialization
This means that, since C23, you may initialize a VLA, but only with an empty initializer to zero-initialize it.
问题已经得到解答,但我想指出另一种解决方案,该解决方案快速且在运行时不改变长度的情况下有效。在 main() 之前使用宏 #define 来定义长度,并且在 main() 中您的初始化将起作用:
宏在实际编译之前运行,长度将是编译时常量(如 David Rodríguez 在他的回答中提到的)。它实际上在编译之前会将 length 替换为 10。
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:
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.