I am trying to create a union to store my color values channel by channel and 4-byte values at the same time. But I am having problems with the order of channels. Different endian machines give different values. Is there any way to create endian proof version of this struct or use different structs when different endians are better for this job? I want to use structs and unions for educational purposes. I am experimenting and want to learn what can and can't do with C structs.
typedef union u_color
{
struct
{
unsigned char blue;
unsigned char green;
unsigned char red;
unsigned char alpha;
};
unsigned int value;
} t_color;
If you want to use it only for getting and setting the
value(or getting colours from thevalue) then (if you use recent gcc family compilers)But for the defining arrays which will represent real screen data structures you need to use one predefined struct layout.
Will always print 0x11223344 despite the endianness.