If I want to replicate a structure in another one (in C), what are the pro&con’s of :
struct1 = struct2;
vs
memcpy(&struct1, &struct2, sizeof(mystruct_t));
Are they equivalent ? Is there a difference in performance or memory use ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
struct1=struct2;notation is not only more concise, but also shorter and leaves more optimization opportunities to the compiler. The semantic meaning of=is an assignment, whilememcpyjust copies memory. That’s a huge difference in readability as well, althoughmemcpydoes the same in this case.Use
=.