我在将结构复制到另一个时遇到问题..你应该帮助我吗:) 我想写出最年轻的人,,
# include <stdio.h>
# include <string.h>
struct person {
char name[16], surname[21];
int age;
};
int main (){
struct person x[3], min;
int i, min_element;
for(i=0;i<3;i++){
gets(x[i].name);
gets(x[i].surname);
scanf("%d", &x[i].age);
fflush(stdin);
}
min_element=x[0].age;
for(i=0;i<3;i++){
if(min_element>x[i].age)
min=x[i]; // here i want to copy structure
}
puts(min.name);
puts(min.surname);
printf("%d", min.age);
return 0;
}
谢谢你...但另一个问题是屏幕上的文本..它就像: F☻ 博!斯 2686740
最佳答案
您的结构具有值语义,即它们没有指针,因此按值复制将做正确的事情,因此您可以简单地说:
myperson1 = myperson2;
关于在 C 中复制结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19734019/