Copy struct to struct in C

前端 未结 7 1745
北荒
北荒 2020-11-27 14:06

I want to copy an identical struct into another and later on use it as a comparance to the first one. The thing is that my compiler gives me a warning when Im doing like thi

7条回答
  •  失恋的感觉
    2020-11-27 14:35

    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.


    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.

提交回复
热议问题