简体   繁体   English

错误:指针与整数之间的比较错误

[英]Error : Wrong comparison between pointer and integer

My code seems fine, but I get this warning (warning: comparison between pointer and integer), what is the best solution to solve this problem? 我的代码看起来不错,但是我得到了这个警告(警告:指针与整数之间的比较),解决此问题的最佳解决方案是什么? I have already used double notation marks for ( char exit = "E" ), also used the same thing with while but the same problem. 我已经对( char exit = "E" )使用了双记号,也对while使用了相同的东西,但存在相同的问题。

#include <stdio.h>

int main()
{
    char c[1];
    char exit = 'E';

    while (c != exit)
    {
        printf("Enter a character\n\n");
        scanf("%s", c);
        printf("your character is : %s\n-------------------\n", c);
    }
}
 #include <stdio.h> int main() { char c[1]; char exit = 'E'; while (c != exit) // here ... { printf("Enter a character\\n\\n"); scanf("%s", c); printf("your character is : %s\\n-------------------\\n", c); } } 

you are trying to compare a char to the pointer the array c decays to. 您正在尝试将char与数组c衰减到的指针进行比较。 What you perhaps wanted to do is to compare the first character of the array to the character exit : 您可能想要做的是将数组的第一个字符与字符exit

while (c[0] != exit)

But that still doesn't make much sense since c is uninitialized and the user not yet had a chance to make any input. 但这仍然没有多大意义,因为c尚未初始化并且用户还没有机会进行任何输入。 Better use a do ... while -loop: 最好do ... while -loop时使用do ... while

#include <stdio.h>

int main()
{
    char c[1];
    char exit = 'E';

    do {
        printf("Enter a character\n\n");
        scanf("%s", c);
        printf("your character is : %s\n-------------------\n", c);
    } while (c[0] != exit);
}

Next thing is, that scanf("%s", c); 接下来的事情是, scanf("%s", c); could fail (yes unlikely, but possible). 可能会失败(是不太可能,但可能)。 And the user could enter more characters than there is room for in the array c . 用户输入的字符数可能会超过数组c中的剩余空间。 You should never use scanf() whithout checking the return value nor "%s" without specifying a WIDTH for the conversion specifier to limit the characters put into the array. 如果不为转换说明符指定WIDTH来限制放入数组的字符,则永远不要使用scanf()检查返回值或"%s"

When reading a string you need memory for WIDTH characters + a terminating '\\0' . 读取字符串时,您需要存储WIDTH字符+一个终止符'\\0' So if you want to read a string of one character, the array has to have at least 2 elements: 因此,如果要读取一个字符的字符串,则数组必须至少包含2个元素:

#include <stdlib.h
#include <stdio.h>

int main()
{
    char c[2];
    char exit = 'E';

    do {
        printf("Enter a character\n\n");
        while (scanf("%1s", c) != 1 ) {
            fputs("Input error!\n");
            return EXIT_FAILURE;
        }
        printf("your character is : %s\n-------------------\n", c);
    } while (c[0] != exit);
}

But if you only want to read one character you are better off with getchar() : 但是,如果您只想读取一个字符,则最好使用getchar()

#include <stdio.h>
{
    int ch;
    while (printf("Enter a character\n\n"),
           (ch = getchar()) != EOF && ch != 'E')
    {
        printf("your character is: %c\n-------------------\n", (char) ch);
    }    
}

i believe this is what you are trying to do. 我相信这就是您想要做的。

warning is because you have not initialzed your character and also you were comparing address of character to character value. 警告是因为您尚未初始化您的字符,并且您正在将字符地址与字符值进行比较。

#include <stdio.h>

int main()
{
    char c[1];
    char exit = 'E';

    while ((c[0]=getchar()) != exit)
    {
        if(c[0]==EOF)break;
        printf("your character is : %c\n",c[0]) ;
    }
    printf("ended");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM