I came across the following code – what is the data type of col_8888 and why does it reference the union _colours? I googled unions, but I can’t find a reference to this kind of declaration – it looks to me as though col_8888 is a “union of unions”?
union _colours {
uint8 c[3][4];
uint32 alignment;
};
static const union _colours col_8888 =
{
{ /* B G R A in memory */
{ 0x00, 0x00, 0xFF, 0xFF, }, /* red */
{ 0x00, 0xFF, 0x00, 0xFF, }, /* green */
{ 0xFF, 0x00, 0x00, 0xFF, }, /* blue */
}
};
#define COL_8888_RED *((uint32 *)&col_8888.c[0])
#define COL_8888_GREEN *((uint32 *)&col_8888.c[1])
#define COL_8888_BLUE *((uint32 *)&col_8888.c[2])
The type of
col_8888isunion _colours, so it isn’t a union of unions: it’s just a union. In C, it is necessary to prefix the union name withunionto use it. Alternatively you can use a typedef. Thus the following two declarations are equivalent: