简体   繁体   English

电话目录程序的“指针与整数之间的比较”错误

[英]'comparison between pointer and integer' error for Phone Directory program

So I'm pretty new too C, and programming. 所以我也很新C和编程。 I'm learning little by little. 我一点一点地学习。 A friend of mine gave me a good idea for a first project. 我的一个朋友给我一个第一个项目的好主意。 The goal is too allow the user too select a department (Phone Directory for a Casino), and once that department has been selected, it will show all the numbers for that department. 目标也是允许用户也选择部门(赌场的电话号码簿),一旦选择了该部门,它将显示该部门的所有号码。

Eventually I want too add more too this, but for now, I'm going too keep it simple. 最终,我也想添加更多,但是现在,我要使其保持简单。

#include <stdio.h>
main()
{
   printf("Please Select Department [Security, Food & Beverage, Hotel]\n");

   char str[20];

   gets(str);

   if (gets(str) == 8)
   {
       printf("You have selected Security\n");
       printf("218-760-1122, 218-760-5564\n");
   }

   else if (gets(str) == 5)
   {
       printf("You have Selected Hotel\n");
       printf("218-443-9810");
   }

   else if (gets(str) == 15)
   {
       printf("You have selected Security\n");
       printf("218-550-9818, 218-443-1231\n");
   }

   return 0;
}

The problem with this program is when I compile and build the program, it prints the "selection" part of the script, where get(str) is used. 该程序的问题是当我编译并生成程序时,它会打印脚本的“选择”部分,其中使用了get(str)。 After I select which one I would like too use, it goes blank, and nothing is displayed, but the script itself is still running. 选择了我也想使用的脚本后,它变成空白,什么也没有显示,但是脚本本身仍在运行。

I'm really new too this, and I'm assuming its something too do with how I'm determining which department is selected, in the if/else statements. 我也真的很新,我假设它在if / else语句中也与我确定选择哪个部门有关。 I cant seem too figure out what I'm doing wrong. 我似乎无法弄清楚我在做什么错。 I've tried different solutions, usually getting different results, the closest being by adding a semi-colon too the last line of the else if statement, which runs the script, skips the first two else statements, and prints the security code. 我尝试了不同的解决方案,通常会得到不同的结果,最接近的解决方案是在运行脚本的else if语句的最后一行加上分号,该脚本运行脚本,跳过前两个else语句,并输出安全代码。

3 warnings pop up for lines 10, 16, and 22 第10、16和22行弹出3条警告

warning: comparison between pointer and integer [enabled by default] 警告:指针与整数之间的比较[默认启用]

This is a learning experience for me, and I would appreciate any help that can be offered. 对我来说,这是一次学习的经历,我将不胜感激。

So, the first and most important error is already in the comments, but repeating it here for completeness: 因此,第一个也是最重要的错误已在注释中,但为了完整起见,在此重复此错误:

Do not use gets() ! 不要使用gets() It is broken and therefore removed from the current C standard. 它已损坏,因此已从当前的C标准中删除。 With gets() , you have no control over how much input is actually read, so any buffer can overflow (and attackers will use that). 使用gets() ,您无法控制实际读取了多少输入,因此任何缓冲区都可能溢出(攻击者将使用该缓冲区)。 The replacement is fgets() , which has a size argument. 替换为fgets() ,它具有一个size参数。

Note the same holds for things like scanf("%s", ...) . 请注意,对于scanf("%s", ...) If you ever need to use a conversion to a string with the scanf family of functions, make sure to read the documentation, it tells you how to use a field width . 如果您需要使用scanf系列函数对字符串的转换,请务必阅读文档,它会告诉您如何使用字段宽度

Back to your code, assuming you use fgets() like you should: 回到您的代码,假设您像应该那样使用fgets()

char str[20];

fgets(str, 20, stdin);

if (fgets(str, 20, stdin) == 8)
{

You have two errors here: 您在这里有两个错误:

  1. You call fgets() twice. 您调用fgets()两次。 Each call will attempt to read a line of input and store it in str . 每个调用将尝试读取输入行并将其存储在str That's not what you want. 那不是你想要的。
  2. fgets() doesn't return the length but a pointer to str if it was successfull . fgets()不返回长度,而是返回str的指针( 如果成功) If it failed, it returns NULL 如果失败,则返回NULL

So, the code should start like this: 因此,代码应该像这样开始:

char str[256];

if (!fgets(str, 256, stdin))
{
    fputs("Error reading input.", stderr);
    return 1;
}

Only after that error check, you should examine the contents of str , ie, what the user actually entered. 仅在进行了错误检查之后,才应检查str内容 ,即用户实际输入的内容。

Note I also increased buffer size. 注意我也增加了缓冲区大小。 People might enter longer lines by accident and there's really no need to be that tight with bytes nowadays. 人们可能会无意间输入更长的行,并且如今确实不需要紧缩字节。

Finally, just checking the length probably isn't a good idea. 最后,仅检查长度可能不是一个好主意。 Instead, include string.h and use the strcmp() function. 而是包括string.h并使用strcmp()函数。

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

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