If I do the following in code for an 8-bit processor:
typedef union
{
unsigned long longn ;
unsigned char chars[4];
} longbytes;
Is longbytes.chars[0] always going to be the lowest byte of longbytes.longn, or does it depend on endianness/compiler/platform/target/luck etc.? I’ve viewed the disassembly of my complied code and that’s how it is in my specific case, but I’m curious if this code is portable.
There are several reasons why this is not portable:
chars[0]addressing the lowest byteunsigned longis not guaranteed to be exactly as long as4chars, so depending on the platform you might not even get the completelong(or sizeof(long) might be smaller then4and you read further, but that’s unlikely for 8Bit processors at least.So all in all that code is not portable at all.