我为什么会遇到这个错误?
implicit declaration of function 'gets' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct myStruct
{
char name[20];
char tel[20];
}contact;
int main(void)
{
contact *mycont[3];
for(int i=0; i<3; i++)
{
mycont[i] = (contact*)malloc(sizeof(contact));
printf("Enter Name Of The Contact No.%d\n",i+1);
gets(mycont[i]->name);
printf("Enter The Contact Telephone Number\n");
gets(mycont[i]->tel);
}
}
stdio.h
中没有函数原型(隐式声明假定为'int'参数)。 - Weather Vanegets()
已被弃用。不建议使用它,因为它被认为是不安全的。在您的程序中,您只需传递接收器数组的名称,而无法知道输入的长度。如果您的用户记住不要写超过20个字符的名称,那么可以使用它。但是,如果用户超过了该限制,gets()
也会超出数组边界继续写入其不拥有的内存。 - user3078414gets()
如此危险,不应该使用。这并没有解释为什么在包含<stdio.h>
的情况下它没有被声明。特别是在C99规则下,它应该被声明——自1999年起,它是C99标准的一部分,并且直到后来才被弃用,在C11中才从标准中删除。要理解发生了什么,我们可能需要更多关于您正在使用的平台(操作系统、版本、C编译器、版本、用于编译的命令行)的信息。 - Jonathan Leffler