且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将结构复制到 C 中的结构

更新时间:2023-02-03 16:19:22

对于简单的结构,你可以像你一样使用 memcpy,或者只是从一个分配到另一个:

For simple structures you can either use memcpy like you do, or just assign from one to the other:

RTCclk = RTCclkBuffert;

编译器将创建代码来为您复制结构.

The compiler will create code to copy the structure for you.

关于复制的一个重要说明:它是一个浅拷贝,就像 memcpy 一样.这意味着如果你有例如一个包含指针的结构,它只会复制实际的指针,而不是它们指向的内容,因此在复制之后,您将有两个指针指向同一内存.

An important note about the copying: It's a shallow copy, just like with memcpy. That means if you have e.g. a structure containing pointers, it's only the actual pointers that will be copied and not what they point to, so after the copy you will have two pointers pointing to the same memory.