c - 为什么这段代码会产生段错误?

标签 c segmentation-fault

我正在编写一个程序,其目标是生成一个随机的三位数,并让用户对其进行 10 次猜测。如果他们在正确的位置猜出正确的数字,则被视为“命中”。如果他们猜出正确的数字但位置错误,则被视为“匹配”。例如,如果要猜测的数字是 123,而您输入 329,则 2 为命中,3 为匹配。到目前为止我的代码如下所示:

#include <stdio.h>
#include <time.h>

#define MIN 100
#define MAX 999

int main()
{
    //Declare variables
    int userDig1 = 0, userDig2 = 0, userDig3 = 0, randDig1 = 0, randDig2 = 0, randDig3 = 0, guesses = 0;

    //Generate random three digit number
    srand(time(NULL)); //seed
    randDig1 = rand() % ((MAX + 1) - MIN) + MIN; //corresponds to the first digit
    randDig2 = rand() % ((MAX + 1) - MIN) + MIN; //corresponds to the second digit
    randDig3 = rand() % ((MAX+ 1 ) - MIN) + MIN; //corresponds to the third digit

    //A for loop that keeps track of the number of guesses
    for (guesses = 1 ; guesses <= 10 ; guesses++)
    {
        //Store user's guess into the appropriate variables
        printf("Enter guess number %d:\n", guesses);
        scanf("%d%d%d\n", userDig1, userDig2, userDig3);

        //Check the user's digits against the actual digits
        if (userDig1 == randDig1) //if first user digit = first actual digit
        {
            printf("Number %d is a hit!\n", userDig1);
        }
        if (userDig2 == randDig2) //if second user digit = second actual digit
        {
            printf("Number %d is a hit!\n", userDig2);
        }
        if (userDig3 == randDig3) //if third user digit = third actual digit
        {
            printf("Number %d is a hit!\n", userDig3);
        }
        if (userDig1 == randDig1 && userDig2 == randDig2 && userDig3 == randDig3) //if all 3 user digits = all 3 actual digits
        {
            printf("Congratulations, you guessed the number %d%d%d\n", userDig1, userDig2, userDig3);
            break;
        }
        if (userDig1 == randDig2 || userDig1 == randDig3) //if first user digit = second or third actual digit
        {
            printf("Number %d is a match!\n", userDig1);
        }
        if (userDig2 == randDig1 || userDig2 == randDig3) //if second user digit = first or third actual digit
        {
            printf("Number %d is a match!\n", userDig2);
        }
        if (userDig3 == randDig1 || userDig3 == randDig2) //if third user digit = first or second actual digit
        {
            printf("Number %d is a match!\n", userDig3);
        }
        else //if none of the user's digits are matches or hits
        {
            printf("None of the digits you entered are matches or hits. Guess again.\n");
        }
    }

    printf("Game over! You failed to guess the correct number.\n");
}

编译并运行后,在输入我的第一个猜测后,出现段错误错误。

现在我知道为什么会发生段错误。这意味着您正在尝试访问您无权访问的内存/值(类似的内容)。但在这种特殊情况下,我不明白什么会导致这种情况发生。如果我不得不冒险猜测,我会说这与我的 scanf 语句有关,我试图通过将每个数字解释为单独的整数来“提取”数字。我知道提取数字的数学方法(除以 10,除以 10),因为我在不同的程序中使用过这个概念,但没有使用循环逻辑。我尝试使用循环来提取数字,但我还没有找到适用于所有数字的方法。那么最终,我的问题是什么,我能做些什么来解决它?

感谢任何和所有帮助。

(此外,我知道这里有一些错误的格式,例如 break 语句位于所有 if 语句的中间。我还将移动此代码到一个单独的函数中,以便用户可以再次玩而无需关闭程序并重新打开它)。

最佳答案

因为您将一个整数传递给 scanf()需要指针的地方。

从整数创建指针是未定义的行为,您还应该传递值的地址,因为它们将被函数修改,正确的使用方法 scanf()在这种情况下是

if scanf("%d%d%d*%c", &userDig1, &userDig2, &userDig3) == 3)
    input_is_correct();
else
    input_is_bad();

请注意,在您的情况下,您正在传递 0scanf()它将尝试取消引用 (void *) 0x00NULL也许。这也是未定义的行为。

使用的指针不是使用 & 获取的变量的地址运算符或 malloc() 的返回值/calloc()/realloc()一般来说是未定义的行为。

关于c - 为什么这段代码会产生段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33049513/

相关文章:

c - do-while 循环是无限的并且不断 append 文件

c - 为什么 Clang 会看到类型冲突?

c++ - 无法将字符串读入矩阵

c - 我在这里遇到段错误

c - 程序尝试写出结果时出现段错误

c - C 语言编程中与 while 循环有关的 True 或 False

c - 如何让子进程交替访问和更改相同的值

c - 双击运行可执行文件

c++ - 为什么会出现段错误?

c++ - 类构造函数 C++ 的段错误