如何修复C代码中的分段错误错误

时间:2014-11-01 23:10:34

标签: c segmentation-fault coredump

我正在为游戏,Mancala编写代码,并在此过程中进行测试。到目前为止,我收到的错误是:

  

分段错误(核心转储)

我不确定如何解决它并保持运行。

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

void move_clockwise(int board[], int pos, int player_1);
void move_count_clock(int board[], int pos, int player_1);
void mancala_board(int board[]);
int *current1;
int *current2;
int board[30], player_1, sum, position, direction, i, n, play, initPos, curPos, pos;

//======================================================================

int main(void)
{
int  board[30];
board[0] = 4;
board[1] = 4;
board[2] = 4;
board[3] = 4;
board[4] = 4;
board[5] = 4;
board[6] = 0;
board[7] = 4;
board[8] = 4;
board[9] = 4;
board[10] = 4;
board[11] = 4;
board[12] = 4;
board[13] = 0;  

printf("Welcome to Mancala\n");
    printf("\n\n      5   4   3   2   1   0   \n");
    printf("BLUE\n");
    printf("=====[%d] [%d] [%d] [%d] [%d] [%d]-----\n", board[5], board[4], board[3], board[2], board[1], board[0]);
    printf("|%d|                  |%d|\n", board[6], board[13]);
    printf("-----[%d] [%d] [%d] [%d] [%d] [%d]=====\n", board[7], board[8], board[9], board[10], board[11], board[12]);
    printf("                  RED\n");
    printf("      6   7   8   9  10   11    \n\n");
sum=board[6]+board[13];
player_1=first_player();

while(sum!=48)
{

    while (player_1 == 1)
    {
    printf("Player RED\n");
    printf("Pick a position corresponding to the integer: ");
    scanf("%d", position);

        while (position!=6 || position!=7 || position!=8 || position!=9 || position!=10 || position!=11)
            {
            printf("\nInvalid input, you have to select a bowl from your side.\nPlease select another position");
            scanf("%d",&position);  
            }
        printf("Would you like to move clockwise or counter-clockwise?");
        printf("\n1. Clockwise \n2. Counter-Clockwise");
        scanf("%d",&direction);
        while(direction!=1 && direction!=2)
            {
            printf("\nInvalid input, you have to select an integer corresponding to the given options.\n");
            printf("\n1. Clockwise \n2. Counter-Clockwise");
            scanf("%d",&direction);
            }
        if (direction==1)
            {
            position+=1;
            move_clockwise(board, position, player_1);
            }
        if(direction==2)
            {
            position=+1;
            move_count_clock(board, position, player_1);
            }
    player_1-=1;
    mancala_board(board);
    }

    if (player_1 == 0)
        {
        printf("Player BLUE\n");
        printf("Pick a position corresponding to the integer: ");
        scanf("%d", position);
        while (position!=0 || position!=1 || position!=2 || position!=3 || position!=4 || position!=5)
            {
            printf("\nInvalid input, you have to select a bowl from your side.\nPlease select another position");
            scanf("%d",&position);
            }
        printf("Would you like to move clockwise or counter-clockwise?");
        printf("\n1. Clockwise \n2. Counter-Clockwise");
        scanf("%d",&direction);
        while(direction!=1 && direction!=2)
            {
            printf("\nInvalid input, you have to select an integer corresponding to the given options.\n");
            printf("\n1. Clockwise \n2. Counter-Clockwise");
            scanf("%d",&direction);
            }
        if (direction==1)
            {
            position+=1;
            move_clockwise(board, position, player_1);
            }
        if(direction==2)
            {
            position=+1;
            move_count_clock(board, position, player_1);
            }
        player_1+=1;
        mancala_board(board);
        }
    sum=board[6]+board[13];
    }
}

//======================================================================

int first_player(void)
{   
    //to determine who will be player 1
    play=rand()%2;  
    return (play);
}

//======================================================================

//Display current board
void mancala_board(int board[])
{
    printf("\n\n      5   4   3   2   1   0   \n");
    printf("BLUE\n");
    printf("=====[%d] [%d] [%d] [%d] [%d] [%d]-----\n", board[5], board[4], board[3], board[2], board[1], board[0]);
    printf("|%d|                  |%d|\n", board[6], board[13]);
    printf("-----[%d] [%d] [%d] [%d] [%d] [%d]=====\n", board[7], board[8], board[9], board[10], board[11], board[12]);
    printf("                  RED\n");
    printf("      6   7   8   9  10   11    \n\n");
}

//======================================================================

//allow player to move again if late marble lands in mancala
//void move_again(int board[], int pos, int player_1)
//{



//}

//======================================================================

//captures the marbles across the current position if player's
//last marble lands on their own bowl with no marbles in
//void capture()
//{



//}

//======================================================================

void move_clockwise(int board[], int pos, int player_1)
{
    initPos = pos;
    n=board[pos];
    pos+=1;
    for (i = 0; i < n ; i++)
    {  
    curPos +=1;     
    board[curPos]+=1;

    if (curPos == 14)
      curPos -=14;

    else if (curPos >= 28)
      curPos -= 28;

    if (player_1 == 0)
    {
    current1 = &(board[curPos]);
    if (current1 == &(board[6]))
        {
        current1 = &(board[7]);
        current1 += 1;
        }
    }    

    if (player_1 == 1)
    {
    current2 = &(board[curPos]);
      if (current2 == &(board[13]))
        {
        current2 = &(board[0]);
        current2 += 1;
        }
    }
  }
  board[initPos] = 0;
}

//======================================================================

void move_count_clock(int board[], int pos, int player_1)
{
    initPos = pos;
    n=board[pos];
    pos+=1;
    for (i = 0; i < n ; i++)
    {  
    curPos +=1;     
    board[curPos]+=1;

    if (curPos == 14)
      curPos -=14;

    else if (curPos >= 28)
      curPos -= 28;

    if (player_1 == 0)
    {
    current1 = &(board[curPos]);
    if (current1 == &(board[6]))
        {
        current1 = &(board[7]);
        current1 += 1;
        }
    }    

    if (player_1 == 1)
    {
    current2 = &(board[curPos]);
      if (current2 == &(board[13]))
        {
        current2 = &(board[0]);
        current2 += 1;
        }
    }
  }
  board[initPos] = 0;
}

1 个答案:

答案 0 :(得分:1)

也许你想把代码分成小块并测试每一块。或许这可以帮到你:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/

放入&amp;内部scanf是正确的建议,我认为这是在评论中。

无论如何,这也很奇怪:

while (position!=6 || position!=7 || position!=8 || position!=9 || position!=10 || position!=11)
{
           scanf("%d",&position);
}

你对此有何看法?它总会进入那个循环,永远不会出现,因为 如果位置为6则不能为7,因此由于OR,该陈述将为真。