我想打印出所有输入的元素,但我的代码会把最近输入的元素打印两次。
以下是我的代码:
例如:
以下是我的代码:
#include<stdio.h>
void f(struct ar *a);
void d(struct ar *a);
struct ar
{
char name[50];
};
int main()
{
struct ar a;
f(&a);
d(&a);
}
void f(struct ar *a)
{
int i;
for(i=0;i<2;i++)
{
printf("enter name:");
gets(a->name);
}
}
void d(struct ar *a)
{
int i;
for(i=0;i<2;i++)
{
puts(a->name);
}
}
例如:
输入
name:john
name:kendall
输出
kendall
kendall
gets()
函数,而应该使用fgets()
函数代替。这是 gcc 给出的警告:“gets
函数很危险,不应该使用。” - Iharob Al Asimia->name
,但没有将其移动到其他地方。那么你期望得到什么? - Eugene Sh.