对于其他空指针也没问题,但核心转储在一个- C

dauxcl2d  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(86)

所以我很难弄清楚为什么我的代码是核心转储。
这是我的代码。

void findMines(Board *b, int x, int y, int row, int column)
{
    while(x != row && y != column)
    {
        if(b->boardSpaces[x][y].mineHere == '\n')
        {
            x = 0;
            y++;
            continue;
        }

        //The most hideous if statement ever.
        if(b->boardSpaces[x][y].mineHere == 'M' || b->boardSpaces[x][y].mineHere == ' ' || b->boardSpaces[x][y].mineHere == '.' || b->boardSpaces[x][y].mineHere == '\n')
        {
            switch(b->boardSpaces[x][y].mineHere)
            {
                case 'M':
                {
                    if(b->boardSpaces[x][y-1].ajacentMines == 0 || b->boardSpaces[x+1][y].ajacentMines == 0 || b->boardSpaces[x][y+1].ajacentMines == 0 || b->boardSpaces[x-1][y].ajacentMines == 0)
                    {
                        b->boardSpaces[x][y].mineHere = '.';
                    }
                    break;
                }

                case ' ':
                {
                    break;
                }
                case '.':
                {
                    break;
                }
            }
            continue;
        }else
        {
            switch(b->boardSpaces[x][y].ajacentMines)
            {
                case 0:
                {
                    if(b->boardSpaces[x][y-1].mineHere == 'M')
                    {
                        b->boardSpaces[x][y-1].mineHere = '.';
                    }
                    if(b->boardSpaces[x+1][y].mineHere == 'M')
                    {
                        b->boardSpaces[x+1][y].mineHere = '.';
                    }
                    if(b->boardSpaces[x][y+1].mineHere == 'M')
                    {
                        b->boardSpaces[x][y+1].mineHere = '.';
                    }
                    if(b->boardSpaces[x-1][y].mineHere == 'M') //core dumps here
                    {
                        b->boardSpaces[x-1][y].mineHere = '.';
                    }
                    break;
                }

                case 1:
                {
                    if(b->boardSpaces[x][y-1].mineHere == '.')
                    {
                        b->boardSpaces[x][y-1].mineHere = 'M';
                        findMines(b, x+1, y,row,column);
                    }

                    if(b->boardSpaces[x+1][y].mineHere == '.')
                    {
                        b->boardSpaces[x+1][y].mineHere = 'M';
                        findMines(b, x+1, y,row,column);
                    }

                    if(b->boardSpaces[x][y+1].mineHere == '.')
                    {
                        b->boardSpaces[x][y+1].mineHere = 'M';
                        findMines(b, x+1, y,row,column);
                    }

                    if(b->boardSpaces[x-1][y].mineHere == '.')
                    {
                        b->boardSpaces[x-1][y].mineHere = 'M';
                        findMines(b, x+1, y,row,column);
                    }
                    break;
                }
            }
            x++;
            continue;
        }
    }
}

字符串
哪块板等于这个...

0 1 .
1 . .
. . 1


我的变量是

x = 0
y = 0
row = 3
column = 3


我试图做的是检查每个空间的右边,左边,上面,下面的指示点,看看是否有一个'M'已经在那里。(在这一点上0,0所以左和上面应该是NULL和右和下面应该是1)。
一开始我以为是因为我的if语句检查的是NULL。

if(b->boardSpaces[x-1][y].mineHere == 'M') //core dumps here


但是所有在它之前的都可以,x,y+1也应该是NULL,所以我迷路了。
如果有关系,这里是我的董事会和我的typedef等东西

/*
The contents of each space in the Mines array can be either
a mine or the amount of mines adjacent to said spot.
*/
typedef union
{
    int ajacentMines; //If not a mine it adds up the amount of mines around it.
    char mineHere; //If mine then it holds char 'M'
}Mine;

/*
Constructs a board with a 2D array for the actual board holding the spots content
See union Mine above.
*/
typedef struct boards
{
    int rows, columns; //rows and columns to make the array
    Mine **boardSpaces; //a void pointer to hold said array
}Board;

xcitsw88

xcitsw881#

如果xy都是0,那么您正在[-1][0]位置进行检查。
这显然超出了界限.
只检查棋盘上的位置-或者,创建一个更大的棋盘,不使用边缘的行和列.这样你就不需要额外的索引检查。

相关问题