I had previously utilised unions with ease; however, I was startled when I read this post and discovered that this code
union ARGB
{
uint32_t colour;
struct componentsTag
{
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
} components;
} pixel;
pixel.colour = 0xff040201; // ARGB::colour is the active member from now on
// somewhere down the line, without any edit to pixel
if(pixel.components.a) // accessing the non-active member ARGB::components
is in fact unclear behaviour
Reading from a member of the union other than the one to whom you have just written results in ambiguous behaviour.
What is the intended use of unions if not this?
Could someone possibly explain it in more detail?