红联Linux门户
Linux帮助

c中gets函数问题

发布时间:2009-04-01 20:25:39来源:红联作者:heiyie
#include
#define N 80
int del(char *p,char x)
{ char *q=p;
for(;*p!='\0';p++)
if(*p!=x)
*q++=*p;
*q='\0';
}
int main()
{ char c[N],*pt=c,x;
printf("enter a string:");
gets(c);
printf("\n");
printf("enter the char deleted:");
x=getchar();
del(pt,x);
printf("the new string is:%s\n",c);
}
程序如上,可以正常运行,但编译时出现这样的警告 warning: the `gets' function is dangerous and should not be used.
为什么阿,
文章评论

共有 3 条评论

  1. ShinyGuo 于 2009-04-02 23:37:05发表:

    因为gets()对输入字符的个数没有限制,所以可能造成缓冲区溢出。应该避免使用它并用fgets()代替。
    char *fgets(char *s, int n, FILE *stream);
    ----cite from

  2. heiyie 于 2009-04-02 22:36:06发表:

    谢谢,那为什么又可以运行么,且好像能输入字符串阿

  3. wang7131984 于 2009-04-01 22:43:03发表:

    Linux 下gcc编译器不支持gets这个函数