I’m trying to learn C and I’ve come across something weird:
struct { int i; double j; } x, y; struct { int i; double j; } z;
Here, you can see I created two structs that are identical in their elements.
Why is it that when I try to assign x = z it will generate a compile error but x = y does not? They have the same contents, so why can’t I assign them back and forth with each other, regardless?
Is there any way I can make this so I can assign x = z? Or do they simply have to be the same struct.
Can any C gurus point me in the right direction?
They have the same content, but not the same type. If they are intended to be of the same type, simply
typedef x z;. If they aren’t the same thing, but just happen to contain the same fields, it’s better to create a separate function that will assign the fields properly.My usual style for declaring structs in C includes the typedef, so I forgot to mention it (sorry!). Here’s the syntax: