在 C 中复制结构

标签 c struct

我在将结构复制到另一个时遇到问题..你应该帮助我吗:) 我想写出最年轻的人,,

# 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/

相关文章:

Java 到 C 对象传递

我可以在 Windows 10 RTM 发布后使用非 EV 证书对驱动程序进行签名吗?

c - 使用指针、标志和函数调用的素数生成器 (C)

python - 将 4 个整数打包为一个字节?

c - Scanf 在 C 中有空格

struct - 如何从 Option::unwrap_or 返回一个新结构?

c - 将多个结构保存到二进制文件中 (C)

c - C 中 3d 数组代码的输出说明

c - 当所有元素都相同时快速排序复杂度?

c++ - 如何在没有 Objective-C 的情况下创建 NSAutoreleasePool?