Why do you have two structures with the same members?
If you have the same type, then
bottom = top;
is a completely natural thing to do.
Printable View
Why do you have two structures with the same members?
If you have the same type, then
bottom = top;
is a completely natural thing to do.
Purely to carry out the exercise of copying one structure to another, the values are irrelevant. Unless you have another way to copy one struct to another?
Well the memcpy() only works because both structures are memberwise alike, which essentially makes one of them redundant. struct assignment which I showed is nothing more than a disguised memcpy anyway.
If you had this say
Then you would have to do memberwise assignments yourself, as inCode:typedef struct one
{
int x;
int y;
} point;
typedef struct two
{
float a;
float b;
} blunt;
bottom.a = top.x;
kinda thing.
yes I take your point - so how would you copy one structure to another
That's it, you've seen both ways.
Generally in the real world you wouldn't have two structs definitions with the same members. You'd have one struct definition and multiple struct variables that are instances of that struct type. E.g. one called "a" and another called "b".
Then the real answer to the question posed becomes:Type safety is a good thing, go with it instead of getting into a habbit of unnecessarily subverting it using memcpy. Otherwise you'll shoot yourself in the foot.Code:a = b;