2016-10-19 60 views
1

這是我的代碼。我在代碼結尾處不斷收到分段錯誤。所有的事情都運行良好,但分段錯誤到最後。CodeBlocks中基於指針的代碼給出分段錯誤

我想準備跳棋遊戲,第一步是用符號初始化每一塊。這段代碼試圖做同樣的事情。

代碼塊版本:13.12 Windows 7的

#include <stdio.h> 
#include <stdlib.h> 

#define boardheight 8 
#define boardwidth 8 
struct Player_symbols{ 
    char symbol; 
    char king_symbol; 
}; 

struct Checker_piece{ 
    char king; 
    int on_board; 
    int num_moves; 
    int player; 
    struct Player_symbols* symbols; 
}; 

int pieces_count[2] = {12,12}; 
struct Checker_piece* player_1_pieces; 
struct Checker_piece* player_2_pieces; 

void initialize_player_pieces(struct Checker_piece* player_pieces, struct Player_symbols* player_symbols, int player_num); 
void initialize_board(struct Checker_piece* checker_board[boardheight][boardwidth],struct Checker_piece* player1,struct Checker_piece* player2); 
void print_board(struct Checker_piece* checker_board[boardheight][boardwidth]); 
int move_piece(struct Checker_piece* checker_board[boardheight][boardwidth], int x,int y,int player); 
void continue_jumping(struct Checker_piece* checker_board[boardheight][boardwidth],int* y,int* x,int player); 
int generate_destination(int x, int *dest_x, int *dest_y, int *dest_y_jump,int dest_y2,int dest_y2_jump,int move_flags,int player); 

int main() 
{ 

    struct Checker_piece* checker_board[boardheight][boardwidth]; 
    struct Checker_piece* dummy; 
    struct Player_symbols* dummy_symbol; 
    //declare and initialize the Checker_piece structures by allocating memory dynamically using a dummy structure to determine its size 
    player_1_pieces = malloc(12*sizeof dummy); 
    player_2_pieces = malloc(12*sizeof dummy); 
    struct Player_symbols *player_1_symbols,*player_2_symbols; 

    player_1_symbols = malloc(sizeof dummy_symbol); 
    player_2_symbols = malloc(sizeof dummy_symbol); 

    //initialize the player symbols 
    player_1_symbols->symbol = 'o'; 
    player_1_symbols->king_symbol = 'O'; 
    player_2_symbols->symbol = 'x'; 
    player_2_symbols->king_symbol = 'X'; 

    initialize_player_pieces(player_1_pieces,player_1_symbols,1); 
    initialize_player_pieces(player_2_pieces,player_2_symbols,2); 

    printf("Done"); 
    return 0; 
} 

void initialize_player_pieces(struct Checker_piece* player_pieces, struct Player_symbols* player_symbols, int player_num){ 
    int i; 
    for (i=0; i<12; i++, player_pieces++) { 
     player_pieces->king='N'; 
     player_pieces->num_moves=0; 
     player_pieces->on_board=1; 
     player_pieces->player=player_num; 
     player_pieces->symbols= player_symbols; 
    } 
} 
+0

'的sizeof dummy_symbol'和'的sizeof dummy'是不是你認爲它是。 –

+0

我會說它與Codeblocks無關。我的猜測是一個超出限制的索引,即超出數組大小的位置。 –

回答

2

check_board被聲明爲指針數組時,它應該struct數組。同樣的dummydummy_symbol(順便說一句,你甚至不需要)。

如果你想要一個符號,那麼malloc的符號,而不是一個指向符號的指針。但是,你甚至都不需要malloc那些:剛剛宣佈基於堆棧的變量:

struct Player_symbols player_1_symbols; // no pointer, no malloc needed 
+0

你能否建議如何爲checker_board聲明結構數組? –

+0

完成...修改代碼...謝謝 –

+0

要聲明'struct'數組,只需將'*'取出。 –

0

我建議你在你的代碼這一細微的變化。 正如你使用的結構,然後動態分配內存到結構而不是指向變量的指針。

struct Checker_piece* checker_board[boardheight][boardwidth]; 



struct Checker_piece* dummy; 
    struct Player_symbols* dummy_symbol; 

    player_1_pieces = (Checker_piece *)malloc(12*sizeof(Checker_piece)); 
    player_2_pieces = (Checker_piece *)malloc(12*sizeof(Checker_piece)); 
    struct Player_symbols *player_1_symbols,*player_2_symbols; 

    player_1_symbols = (Player_symbols *)malloc(sizeof(Player_symbols)); 
    player_2_symbols = (Player_symbols *)malloc(sizeof(Player_symbols)); 

PS-你並不需要強制類型轉換的malloc,如果你是用C programmimg