gpt4 book ai didi

c - 使用memset来防止 ''可变大小的对象可能无法初始化'

转载 作者:行者123 更新时间:2023-11-30 20:04:20 26 4
gpt4 key购买 nike

我尝试使用下面的代码将值设置为 2D 数组(设想游戏板或某个网格),但收到“可变大小的对象可能未初始化”错误。

我尝试使用memset解决这个问题,但没有成功。

非常感谢,如果有一些提示,我们将不胜感激。

<小时/>
// constants
#define DIM_MIN 3
#define DIM_MAX 9

// board
int board[DIM_MAX][DIM_MAX];


void init(void)
{
int highest = d^2;

for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
int board[i][j] = highest - 1;
}
}
if (d % 2 == 0)
{
int board[d-1][d-2] = 2;
int board[d-1][d-3] = 1;
}
}

最佳答案

问题出在 for 循环体中的代码,上面写着

  int board[i][j] = highest - 1;

你想要的可能是

  board[i][j] = highest - 1;

if 条件体也是如此。

为了详细说明错误消息,int board[i][j] =最高 - 1; 尝试定义一个新的 VLA (variable length array) ,无法初始化。

引用 C11,第 §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.

关于c - 使用memset来防止 ''可变大小的对象可能无法初始化',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41527142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com