gpt4 book ai didi

objective-c - exc_bad_access(代码= 2 地址= 0x0)

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:02 25 4
gpt4 key购买 nike

我是新手,正在尝试制作扫雷 iphone 应用

我使用了 IBButton 来重置雷区这是结构的 2 x 2 矩阵

- (IBAction) Reset {
for (int x = 0 ; x < 10 ; x ++) {
for (int y = 0 ; y < 10 ; y++ ) {
f[x][y]->isOpen = NO;
f[x][y]->display = 0; //Going to make a search function for finding Number of mines next to it
int random = arc4random()%10;
if (random < 2) {
f[x][y]->isMine = YES;
} else {
f[x][y]->isMine = NO;
}
}
}

所以我在 for 循环的第一行得到了错误f[x][y]->....

我做错了什么?

/编辑

我就是这样声明我的f的

struct feild *f[10][10];
struct feild{
bool isOpen;
bool isMine;
int display;
}

最佳答案

你没有为 f 分配任何空间,所以 f[x][y] 将只包含垃圾内存,然后 ->isOpen = NO 访问将炸毁。

你需要做类似的事情

for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
f[i][j] = malloc(sizeof(struct feild));
}
}

在你的代码之前。

关于objective-c - exc_bad_access(代码= 2 地址= 0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14947046/

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