c - 警告 : comparison b/w pointer and integer in C.

标签 c

int main()
{
    char name[10], food[10], color[10], ans[10];
    int height, year, age, date; 

    /* Ask for user inputs and do stuff here */
    /* ------------------------------------- */

    printf("Would you like me to repeat this?\n");
    scanf("%c", ans);   

    /* its giving me a warning for the if condition on the line  */ 
    /* below saying warning: comparison b/w pointer and integer. */

    if  (ans == 'y')
        printf("ok I will\n");
    else
        printf("fine.\n");

    return 0;
}

在程序的最后,我写了一个 IF 语句,如果用户输入“y”,那么它会说“好的,我会的”,否则会说“好”——没什么特别的。 但是当我运行它时,程序会问我“你想让我重复一遍吗?”即使我输入“y”,程序也会输出“好”而不是“好的,我会”……请帮忙。

最佳答案

变量 ans 是一个数组,可以衰减为指向第一个元素的指针。你把它比作一个角色。

你应该改成例如ans[0] == 'y'

或者更好的是,既然你只读了一个字符,为什么还要我们一个数组呢?只需将 ans 声明为单个字符,并在 scanf 调用中使用 address-of 运算符:

char ans;

scanf(" %c", &ans);

if (tolower(ans) == 'y') ...

关于c - 警告 : comparison b/w pointer and integer in C.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32542921/

相关文章:

c - c程序中的链接器错误

C - 如何检查不需要的字符是否在数组中?

c++ - Pi 计算器程序每次运行时都会给出不同的输出

c - 如何使用调试器 (gdb) 查看进程的分支预测器表?

c - 在附加 header 中包含 header 包含路径

c++ - cuda 文件没有链接到 C 文件中定义的函数

java - 传递给 C 的 JNI 回调

c - POSIX C 线程中上下文切换之前当前堆栈指针寄存器的值存储在哪里

c++ - 如何将 C 程序转换为类

c - 非同质数组的内存分配是如何工作的?