Copying structure members

前端 未结 4 1193
既然无缘
既然无缘 2021-01-28 04:46

I am more confused with structures. There are many ways to copy a structure.

struct complex
{
   int real;
   int imaginary;
};

Assume c1 is complex structure v         


        
4条回答
  •  无人共我
    2021-01-28 05:39

    memcpy(&c1.real, &c2->real, sizeof(struct complex)) is very strange, since bots types are atomic, there is no need for that kind of copying. Also sizeof(struct complex) is wrong, because it should have been sizeof(int) (which is only 1/2 size of the whole structure) Simple assignment is enough:

    c1.real = c2->real; 
    

提交回复
热议问题