简体   繁体   English

如何将typedef结构复制到另一个?

[英]How to copy typedef struct into another one?

How to copy typedef struct into another one? 如何将typedef结构复制到另一个?

If I have a typedef struct called books and I have a struct variable called books first . 如果我有一个名为books的typedef结构,而我有一个名为books first的struct变量。 Now I declare books second How can I pass the content of first to second? 现在我将books second声明为books second如何将第一books second的内容传递给第二books second Is there a function to do that or I can do that just with second = first ? 是否有一个功能可以做到这一点,或者我可以仅使用second = first来做到这一点?

If both first and second have the same type, then you can just do second = first; 如果firstsecond具有相同的类型,则可以只执行second = first; . It does not matter whether the type is an built-in or user-defined. 类型是内置的还是用户定义的都没有关系。 C will copy the contents of first over to second . C将first的内容复制到second Just try it. 去尝试一下。

In general, variables in C are just data with a name and a type. 通常,C语言中的变量只是具有名称和类型的数据。 If the types of 2 variables a and b match, you can assign one to the other: a = b; 如果两个变量ab的类型匹配,则可以将一个分配给另一个: a = b; . What happens is that the value of variable b is copied into variable a . 发生的情况是将变量b的值复制到变量a

But beware of pointers: For C, pointers are just variables with a value (the fact that the value represents a memory address does not matter, C treats all data equal). 但是要当心指针:对于C,指针只是带有值的变量(值表示内存地址的事实并不重要,C会将所有数据视为相等)。 If the 2 variables happen to be pointers, like char *a; char *b; 如果这两个变量恰好是指针,例如char *a; char *b; char *a; char *b; then you can assign a = b; 那么您可以分配a = b; just with any variable. 与任何变量。 But since the value of the variable b is the memory address, the memory address is copied from b to a , not the content of the memory at the memory address. 但是由于变量b的值是内存地址,因此内存地址将从b复制到a ,而不是内存地址处的内存内容。

If you want to have the memory copied over, you will have to do it on your own, eg via the help of memcpy() (see its man page for information). 如果要复制内存,则必须自己完成操作,例如,借助memcpy() (有关信息,请参见其手册页)。

That said, if your structs contain pointers, the pointers are the content, not the stuff the pointers point to. 也就是说,如果您的结构包含指针,则指针是内容,而不是指针指向的内容。 C would copy the pointer values, not the pointer targets. C将复制指针值,而不是指针目标。 If you got pointers in your structs and want some sort of deep-copy, you would have to implement the traversal of your structs on your own. 如果结构中有指针并需要某种深层复制,则必须自行实现结构的遍历。 See What is the difference between a deep copy and a shallow copy? 请参阅深层副本和浅层副本有什么区别?

If your structure does not contain any members that are pointers (or containing structs that do), then you can simply assign one to the other. 如果您的结构不包含任何指针成员(或包含包含指针的成员),则可以简单地将一个分配给另一个。

second = first;

If however your structs do contain pointers, then with a simple assignment you end up with two struct that contain pointers to the same memory, and changing one will affect the other. 但是,如果您的结构确实包含指针,那么通过简单的赋值,您将得到两个包含指向同一内存的指针的结构,并且更改其中一个将影响另一个。 If that's not what you want then you need to do a deep copy. 如果这不是您想要的,则需要进行深度复制。

For example: 例如:

struct book {
    char *name;
    int cost;   // in cents, so you don't have to deal with floating point issues
};

struct book first;
first.name = strdup("title1");
first.cost = 500;

struct book second;
second.name = strdup(first.name);
second.cost = first.cost;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM