C 生命指针游戏

C 生命指针游戏,c,pointers,multidimensional-array,C,Pointers,Multidimensional Array,我正在用C语言创建生活游戏,在程序从用户那里获取输入后,我得到了这个分段错误。我最近开始学习C语言,我对指针的理解是基本的。我在网上查找并尝试了不同的方法使其正确后,还没有找到解决方法。如果我不使用指针并保持简单,一切正常。我将感谢任何帮助 int main() { int maxR; int maxC; int generations; int i=0; int j=0;

我正在用C语言创建生活游戏,在程序从用户那里获取输入后,我得到了这个分段错误。我最近开始学习C语言,我对指针的理解是基本的。我在网上查找并尝试了不同的方法使其正确后,还没有找到解决方法。如果我不使用指针并保持简单,一切正常。我将感谢任何帮助

   int main() {

    int maxR;               
    int maxC;               
    int generations;        
    int i=0;
    int j=0;
    int k=0;
    int n;                  //neighbour count
    char state;
    char **board;         //original boardfor comparison
    char **newBoard;        //boardto make changes to

    scanf("%d %d %d",&maxR,&maxC,&generations);             //take input

    board= (char**)malloc(maxR * sizeof(char*));         //allocating memory
    newBoard=(char**) malloc(maxR * sizeof(char*));        //allocating memory

    for(i=0; i<maxR; i++) {
        board[i] = malloc(maxC * sizeof (char));          //allocating memory
        newBoard[i] = malloc(maxC * sizeof (char));         //allocating memory
        for(j=0; j<maxC; j++) {
            scanf (" %c", &board[i][j]);                  //getting input
        }
    }

    for(i=0; i<=generations; i++ ) {                        
        for (j=0; j<maxR; j++) {                            
            for (k=0; k<maxC; k++) {                        
                state=board[j][k];                        
                n=countNeighbours(board,maxR,maxC,j,k);   

                if(state == '1') {                          //if the cell is alive
                    if(n==2 || n==3) newBoard[j][k] = '1';  //if the cell has 2 or 3 neighbours then it lives
                    else newBoard[j][k]='0';                //else the cell dies

                } else {                                     //else (if) the cell is dead
                    if(n==3) newBoard[j][k]='1';            //but has 3 neibours then the cell become alive
                    else newBoard[i][j]='0';                //else it dies
                }
            }
        }
        memcpy(board, newBoard,sizeof(board));          //copy the updated grid to the old one
    }

    printBoard(board,maxR,maxC);                          

    deallocate(board,maxR);                               //deallocatethe memory
    deallocate(copyGrid,maxR);                              //deallocatethe memory

这里有一个明显的问题

memcpy(oldGrid, copyGrid,sizeof(oldGrid));          //copy the updated grid to the old one
由于oldGrid是一个char**指针,因此SizeFoldGrid是指针的大小,根据您的平台可能是4或8个字节。所以,你没有复制网格,你只是复制了它的几个字节

如果要复制整个网格,需要计算网格的大小(以字节为单位)


如果oldGrid被声明为数组而不是指针,那么SizeFoldGrid将产生您所期望的网格的完整大小。数组在sizeof方面的行为与指针不同。

是的,简单地说-您需要将实例的大小放在那里,而不是指针的大小,这就是您现在要做的。很高兴你有了sizeof,你会得到指针的大小,而不是网格的大小。谢谢你的回复。如果我使用两个for循环来将一个网格复制到另一个网格,而不是memcpy,那么在使用户input@James,可能在主函数的第二个for循环中。”else copyGrid[i][j]='.';'这就是问题所在。您的意图是“else copyGrid[j][k]=”;“?哈?@Kay是的,你是对的,解决了这个问题。非常感谢。