2

我是新手,正在尝试制作扫雷 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;
}
4

1 回答 1

1

您还没有为 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));
    }
 }

在你的代码之前。

于 2013-02-19T00:11:37.213 回答