I have 2 identically-sized structs. Their members will never change. The size of their data types is known on the target platform. Is there a reason to not ever do this:
struct foo
{
unsigned int a; //4 bytes
};
struct bar
{
unsigned short a; //2 bytes
unsigned short b; //2 bytes
}
...
foo var1;
bar var2 = *((bar*)((unsigned short*)&foo)); //will this ever cause a runtime error?
I'm not asking if there's a different way to do it (memcpy), or a more readable way, etc...just wondering if this will ever cause an actual error. The code compiles...but I want to make sure I'm not setting myself up for failure later...