基于指针的代码在CodeBlocks中给出Segmentation fault

时间:2016-10-19 16:24:58

标签: c pointers codeblocks

这是我的代码。我在代码结束时不断遇到分段错误。所有事情都运行良好,但分段错误最终会出现。

我正在尝试准备跳棋游戏,第一步是用符号初始化每个棋子。此代码尝试执行相同的操作。

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;
    }
}

2 个答案:

答案 0 :(得分:2)

指针 同样适用于check_boardstruct(顺便说一下,您甚至不需要)。

如果你想dummy一个符号,那么dummy_symbol一个符号,而不是一个指向符号的指针。但你甚至不需要malloc那些:只声明基于堆栈的变量:

malloc

答案 1 :(得分: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-如果您是C语言程序,则不需要键入cast malloc。