GCC编译错误:格式'%c'需要类型'char*'的参数,但参数2的类型为'int'[-Wformat]

Nyx*_*yxm 6 c gcc char

好吧,我是C的菜鸟,但我认为代码是基本的,直截了当的.该程序适用于大学作业,并且应该具有'isdigit()'功能.这是代码

//by Nyxm
#include <stdio.h>
#include <ctype.h>

main()
{
    char userChar;
    int userNum, randNum;
    srand(clock());
    printf("\nThis program will generate a random number between 0 and 9 for a user to guess.\n");
    /*I changed it from '1 to 10' to '0 to 9' to be able to use the isdigit() function which
    will only let me use a 1 digit character for an argument*/
    printf("Please enter a digit from 0 to 9 as your guess: ");
    scanf("%c", userChar);
    if (isdigit(userChar))
    {
            userNum = userChar - '0';
            randNum = (rand() % 10);
            if (userNum == randNum)
            {
                    printf("Good guess! It was the random number.\n");
            }
            else
            {
                    printf("Sorry, the random number was %d.\n", randNum);
            }
    }
    else
    {
            printf("Sorry, you did not enter a digit between 0 and 9. Please try to run the program again.\$
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译时,我收到以下错误

week3work1.c: In function ‘main’:
week3work1.c:14:2: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)

到底是怎么回事?我迫切需要帮助.任何帮助都可以.我真的很想放弃这个计划.当我的教科书显示"%c"是针对普通的'char'时,为什么说它期望'char*'的论点?我使用nano,gcc和Ubuntu,如果这有任何区别.

Dan*_*ego 14

因为scanf(),你需要将指针传递给a char,否则它没有任何方法来存储字符,因为所说的char将通过值传递.所以你需要&userChar代替.

假设userChar是0在通话之前.使用您当前的代码,您基本上就是这样做(就实用程序而言):

scanf("%c", 0);
Run Code Online (Sandbox Code Playgroud)

你想要的是这个:

scanf("%c", some-location-to-put-a-char);
Run Code Online (Sandbox Code Playgroud)

这是&userChar.

提到的man页面scanf:

   c      Matches  a  sequence  of characters whose length is specified by
          the maximum field width (default 1); the next pointer must be  a
          pointer to char, and there must be enough room for all the char?
          acters (no terminating null byte is added).  The usual  skip  of
          leading  white  space is suppressed.  To skip white space first,
          use an explicit space in the format.
Run Code Online (Sandbox Code Playgroud)