[英]exc_bad_access (code= 2 address =0x0)
I am new to this, trying to make an Minesweeper iphone app 我对此很陌生,尝试制作一款扫雷iPhone应用程序
i used a IBButton to Reset mine fields which is a 2 by 2 matrix of a struct 我使用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;
}
}
}
so i get the the error at the very first line of my for loop f[x][y]->.... 所以我在for循环f [x] [y]->的第一行得到了错误。
what did i do wrong here? 我在这里做错了什么?
/edit /编辑
This is how i declared my f 这就是我宣布我的f的方式
struct feild *f[10][10];
struct feild{
bool isOpen;
bool isMine;
int display;
}
You haven't allocated any space for f, so f[x][y]
will just contain junk memory and then the ->isOpen = NO
access will blow up. 您尚未为f分配任何空间,因此
f[x][y]
仅包含垃圾内存,然后->isOpen = NO
访问将->isOpen = NO
。
you need to do something like 你需要做类似的事情
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
f[i][j] = malloc(sizeof(struct feild));
}
}
before your code. 在您的代码之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.