C的分段错误

时间:2013-10-11 22:36:34

标签: c segmentation-fault

我的程序是一个简单的“岩纸剪刀蜥蜴Spock”​​程序,我想添加一个2人或1个玩家与计算机选项。这就是我对选择部分进行编码的方式:

printf("1.One player OR 2.Two players?\n");
scanf("%d", playerNum);
//input section
printf("1.rock\n2.paper\n3.scissor\n4.spock\n5.lizard\n");
printf("player A enter a choice ");
scanf ("%d", &choiceA);
if (playerNum == 2)
{
    printf("1.rock\n2.paper\n3.scissor\n4.spock\n5.lizard\n");
    printf("player B enter a choice ");
    scanf ("%d", &choiceB);
}
else if (playerNum == 1)
{
    choiceB = 1+rand()%5;
    printf("The computer has picked %d", choiceB);
}

当我运行程序时,在输入playerNum的值后,它立即给我一个分段错误
上面使用的所有变量都声明为整数。

1 个答案:

答案 0 :(得分:7)

当您scanf传递int *类型参数时,int期望参数&。{ 您在scanf的参数中缺少scanf("%d", playerNum); ^ | & is missing.

scanf("%d", &playerNum);

将此更改为

{{1}}