C:为什么我会收到分段错误(核心转储)错误

时间:2016-12-27 00:47:20

标签: c

这是我的完整代码。任何帮助将非常感激。

(常量在这里定义)

结构

typedef struct word{
    char name[16];
    char orientation;
    int row;
    char col;
    int player;
    int points;
}WORD;

typedef struct piece{
    char letter;
    int points;
}PIECE;

typedef struct player{
    char name[20];
    int points;
    PIECE hand[7];
}PLAYER;

typedef struct game{
    WORD words[50];
    PLAYER players[2];
    PIECE pool[115];
    char board[15][15];
    char dictionary [27718][16];
    char turn;
    int word_count;
}GAME;

用于显示电路板的功能。必须获得GAME类型结构     void show_board(游戏游戏){

    int i,j,row,col,orient;
    int colour,letter1,letter2;
    int board[17][17] = {{-1,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,-1},
                         (...)
{-1,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,-1}};  

    char game_board[BOARD_SIZE][BOARD_SIZE];
    for(i=0;i<BOARD_SIZE;i++)
    {
        for(j=0;j<BOARD_SIZE;j++)
            game_board[i][j] = ' ';
    }

    for(i=0;i < game.word_count;i++)
    {
        row = game.words[i].row - 1;
        col = game.words[i].col - 'A';
        orient = game.words[i].orientation; 

        for(j=0;game.words[i].name[j] != '\0';j++)
        {
            game_board[row][col] = game.words[i].name[j];
            if(orient == 'v')
                row++;
            else if(orient == 'h')
                col++;
        }       
    }   

    for(row = 0;row < MAX_ROWS;row++)
    {
        for(col = 0;col < MAX_COLS;col++)
        {
            switch(board[row][col]){    
                case WHT: colour = BG_WHITE;break;
                case RED: colour = BG_RED;break;
                case LBL: colour = BG_CYAN;break;
                case PNK: colour = BG_MAGENTA;break;
                case BLU: colour = BG_BLUE;break;
                case YEL: colour = BG_YELLOW;break;
                default: colour = BG_BLACK;break;
            }
            if((row == 0 &&  col == 0) || row == 0 &&  col == 16 || row == 16 &&  col == 0 || row == 16 &&  col == 16){
                printf("%c[%d;%dm %c%c %c[%dm",ESCAPE,FONT_WHITE,colour,' ',' ', ESCAPE, 0);    
            }else if((row == 0 || row == 16) && (col!= 0 || col!= 16)){
                letter1 = board[row][col];      
                printf("%c[%d;%dm %c %c[%dm",ESCAPE,FONT_WHITE,colour,letter1, ESCAPE, 0);
            }else if((col == 0 || col == 16) && (row != 0 || row != 16)){               
                if(board[row][col] > 9)
                {
                    letter1 = 1 + '0';
                    letter2 = board[row][col]%10;
                    letter2 = letter2 + '0';
                }else{
                    letter2 = board[row][col] + '0';
                    letter1 = ' ';
                }                                   
                printf("%c[%d;%dm %c%c %c[%dm",ESCAPE,FONT_WHITE,colour,letter1,letter2, ESCAPE, 0);                            
            }else{
                letter1 = ' ';
                if(game_board[row-1][col-1]!=' '){
                    letter1 = game_board[row-1][col-1];
                }       
                if(letter1 != ' ')
                    colour = BG_YELLOW;     
                printf("%c[%d;%dm %c %c[%dm",ESCAPE,FONT_BLACK,colour,letter1, ESCAPE, 0);  
            }           
        }
        putchar('\n');      
    }   
    putchar('\n');  
}

MAIN     int main(){     游戏游戏;     show_board(游戏); }

1 个答案:

答案 0 :(得分:0)

我不确定它是否会在此满足,但您可以为此array动态分配所有structure的内存。通过这样做,您将在heap中分配内存,这样做可能会阻止您stackoverflow

提供一个链接。请测试一下。

http://www.geeksforgeeks.org/struct-hack/