6

I once baffled a colleague by showing him that you don't have to (and shouldn't!) use memcpy to copy struct-instances into a different variable in C, which in turn baffled me.

Good times!

Comments
  • 0
    What do you mean by "shouldn't"? I feel this needs more context...
    You can indeed copy structs or other objects directly e.g. if they're not accessed via a pointer. For example:

    struct some_struct_type s1;
    // Do something with s1
    struct some_struct_type s2 = s1;

    This way, the compiler handles the copying for you, and it's a bit "safer" than using memcpy.
Add Comment