C 中的 gets() 问题
我编写了以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 128
int main ()
{
char mychar , string [SIZE];
int i;
int const count =0 ;
printf ("Please enter your string: \n\n");
fgets (string, SIZE, stdin);
printf ("Please enter char to find: ");
mychar = getchar();
for (i=0 ; (string[i] == '\0') ; i++ )
if ( string[i] == mychar )
count++;
printf ("The char %c appears %d times" ,mychar ,count);
return 0;
}
问题是 gcc 给了我一个关于 'int const count' 的错误:“只读变量 'count' 的增量”。
好像出了什么问题?
感谢 !
I wrote the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 128
int main ()
{
char mychar , string [SIZE];
int i;
int const count =0 ;
printf ("Please enter your string: \n\n");
fgets (string, SIZE, stdin);
printf ("Please enter char to find: ");
mychar = getchar();
for (i=0 ; (string[i] == '\0') ; i++ )
if ( string[i] == mychar )
count++;
printf ("The char %c appears %d times" ,mychar ,count);
return 0;
}
The problem is that the gcc gives me an error for the 'int const count': " increment of read-only variable ‘count’".
What seems to be wrong ?
Thank !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试使用 fgets 代替:
为什么
gets
不安全,已在 SO 上多次回答。您可以看到这个。Try using fgets instead as:
Why
gets
is unsafe, has been answered several times on SO. You can see this.始终使用
fgets()
而不是gets
。还有很多东西需要修复。您不应该使用标准库函数来创建用户界面。标准库确实不是为此设计的。相反,您应该使用 curses 库 或类似的东西。您还可以编写程序来接受参数作为输入。正确使用标准库的简短示例。该版本没有任何错误检查,因此假设用户输入是正确的。
用法:
./count "Hello, World!" l
输出:
字符“l”出现3次。
编辑:至于原始代码。将
==
更改为!=
。to:
比较是错误的。
Always use
fgets()
instead ofgets
. Also there are lots of stuff to fix. You shouldnt use standard library functions for creating user interface. Standard library is really not designed for that. Instead you should use curses library or something similar. You could also write the program to accept arguments as input.Short example of proper use of the standard library. This version does not have any error checking so it assumes that user input is correct.
Usage:
./count "Hello, World!" l
Output:
The char ‘l’ appears 3 times.
EDIT: As for the original code. Change
==
to!=
.to:
The comparison was wrong.
要使此示例正常工作,您还应该将以下行:
if(*string == mychar) ++count;
更改为
if(string[i] == mychar) ++count;< /code>
完整的工作示例现在是:
To make this example work you should also change the line:
if(*string == mychar) ++count;
into
if(string[i] == mychar) ++count;
Full working example is now:
考虑替换为“
scanf(“%s”, &string)
”。Consider replace with "
scanf( "%s", &string)
" instead.gets 是危险的,因为它允许您读入的数据多于分配的空间,您可以使用 fgets 来指定要读入的字符数,并在发现换行符时停止。
gets is dangerous because it lets you read in more data than you've allocated space for, you can use fgets which specifies how many characters it is going to read in and stops if it finds a newline.
gets 是危险的,因为它可以获取比变量大小更多的数据。从而使系统遭受攻击并损害安全性。
应该使用 fgets,因为它限制了数量。要读取的字符数。
gets is dangerous because it can take in more data than the size of the variable. Thereby exposing the system to attacks and compromising security.
fgets should be used as it limits no. of characters to be read.
这会做:
This will do: