Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 菜单功能错误:指针和整数之间的比较[默认启用]_C_Eclipse Cdt - Fatal编程技术网

C 菜单功能错误:指针和整数之间的比较[默认启用]

C 菜单功能错误:指针和整数之间的比较[默认启用],c,eclipse-cdt,C,Eclipse Cdt,我有一个菜单功能。编译后,以下错误不断出现:错误:指针和整数之间的比较[默认启用]。为什么会这样 char choice; printf ("Welcome to the Customer menu! \n"); printf ("Please select option from below\n"); printf ("a. Add customer\n"); printf ("b. Modify customer\n"); printf ("

我有一个菜单功能。编译后,以下错误不断出现:错误:指针和整数之间的比较[默认启用]。为什么会这样

    char choice;

    printf ("Welcome to the Customer menu! \n");
    printf ("Please select option from below\n");
    printf ("a. Add customer\n");
    printf ("b. Modify customer\n");
    printf ("c. List customers\n");
    printf ("d. Go back to main menu");

    while ((gets(&choice)) != 'q')
            {
                if (choice == '\n')
                    continue;
                switch (choice)
                {

        case 'a' : add_customer();
                   break;
        case 'b' : printf ("products_main ()");
                   break;
        case 'c' : printf ("orders_main ()");
                   break;
        default : printf ("Invalid input. Please enter an option from the above menu\n");
                  continue;

                }

                printf ("END PROGRAM");
谢谢

这一行:

while ((gets(&choice)) != 'q')
gets()读取字符串,而不是字符,并返回该字符串(即,它填充通过字符指针传递给它的缓冲区)。然后将返回的指针(与传入的指针相同)与字符进行比较

你可能只想读一个字符。如果需要整个字符串,则需要将其读入字符数组,而不是传递单个字符的地址。

函数
get()
返回一个
char*
,而您将该返回值与
char
进行比较:

if (gets(&choice)) != 'q')
还请注意,这在两个级别上都是错误的,因为
get()
stdin
读取,直到遇到换行符,因此如果将一个
char
的地址传递给它,可能会导致缓冲区溢出错误。为什么不改用
fgets()

char buf[128];
fgets(buf, sizeof(buf), stdin);
if (buf[0] == 'q') {
    /* etc */
}
您不能使用gets()来执行此操作,毕竟gets()是非常危险的,它不检查要读取的字符数,因此可能会导致非常严重的运行时缓冲区溢出

您应该像H2CO3一样使用fgets(),因为它有读取字符的限制,所以更安全

char * input(const char *message, size_t quantity)
{
    const int BUFFER_SIZE = 512;
    char buf[BUFFER_SIZE], *res = NULL;

    if(quantity > BUFFER_SIZE || quantity == 0)
        quantity = BUFFER_SIZE - 1;

    if(message) 
        printf("%s",message);

   if(fgets(buf, quantity + 1, stdin) > 0)
   {
        char *end = strchr(buf, '\n');
        if(end){
            *end = '\0';
        }

        res = malloc(strlen(buf) + 1);
        if(!res)
        {
           fprintf(stderr, "input(): MEM alloc error\n");
           return NULL;
        }
       strcpy(res, buf);

   }
   return res;
}
尝试使用该功能,只需传递所需的消息,以及所需输入的确切字符数。:)

如果您想单独尝试,这里有一个测试程序:

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

char * input(const char *message, size_t quantity)
{
    const int BUFFER_SIZE = 512;
    char buf[BUFFER_SIZE], *res = NULL;

    if(quantity > BUFFER_SIZE || quantity == 0)
        quantity = BUFFER_SIZE - 1;

    if(message) 
        printf("%s",message);

   if(fgets(buf, quantity + 1, stdin) > 0)
   {
        char *end = strchr(buf, '\n');
        if(end){
            *end = '\0';
        }

        res = malloc(strlen(buf) + 1);
        if(!res)
        {
           fprintf(stderr, "input(): MEM alloc error\n");
           return NULL;
        }
       strcpy(res, buf);
   }
   return res;
}

int main()
{
    char *a = input("Input:", 4);
    if(a) 
    {
        printf("%s\n",a);   
        free(a);   
        return 0;
    }
    printf("Got NULL input\n");
    return -1;
}
#包括
#包括
#包括
字符*输入(常量字符*消息、大小和数量)
{
const int BUFFER_SIZE=512;
char buf[缓冲区大小],*res=NULL;
如果(数量>缓冲区大小| |数量==0)
数量=缓冲区大小-1;
如果(信息)
printf(“%s”,消息);
如果(fgets(buf,数量+1,标准输入)>0)
{
char*end=strchr(buf,'\n');
若(完){
*结束='\0';
}
res=malloc(strlen(buf)+1);
如果(!res)
{
fprintf(stderr,“input():MEM alloc error\n”);
返回NULL;
}
strcpy(res,buf);
}
返回res;
}
int main()
{
char*a=输入(“输入:”,4);
如果(a)
{
printf(“%s\n”,a);
免费(a);
返回0;
}
printf(“获取空输入\n”);
返回-1;
}
当你对一个特定的函数有疑问时,它们的参数是什么,它们的返回值是什么,你可以在谷歌上查找,你会发现很多例子和函数定义。随着时间的推移,您将学会轻松理解定义并记住一些函数名及其参数


祝你好运

读了一些书后,我发现

#include <unistd.h>
#包括
有助于避开警告。我是unix c新手,以前从未见过它。我还在测试我的代码,所以当我弄清楚这是否有效时,我会给你回复

希望这有帮助

最后警告回来了,它以无限循环结束,所以我的逻辑出了问题


对不起,我帮不上忙

顺便问一下,你为什么不看看你要使用的函数的文档呢?