c - C的段错误

标签 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 输入一个值后立即出现段错误
上面使用的所有变量都声明为整数。

最佳答案

你的 scanf 需要一个 int * 类型的参数,而你正在传递它 int 类型的参数。
您在 scanf 的参数中缺少 &

scanf("%d", playerNum);
           ^
           |
        & is missing.  

将此更改为

scanf("%d", &playerNum);

关于c - C的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19328314/

相关文章:

c - C 中的指针和段错误

c - 从不兼容的指针类型传递

c - 为什么我的十六进制转换功能差了一个?

c - 西里尔文的问题,搜索字符串

无法弄清楚为什么我在初始化数组时遇到段错误

android - 在由 Android 应用程序链接的 native 库中调试崩溃的最简单方法?

c++ - glibc 检测到错误

c - 运行 for 循环时出现奇怪的输出

c 使用 fgets、strtok 读取文件导致段错误

c++ - Vector.erase 中的 SIGSEGV