tags:

views:

84

answers:

1

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])
+8  A: 

The type of col_8888 is union _colours, so it isn't a union of unions: it's just a union. In C, it is necessary to prefix the union name with union to use it. Alternatively you can use a typedef. Thus the following two declarations are equivalent:

union _colours {
    uint8  c[3][4];
    uint32 alignment;
};

static const union _colours col_8888 =
...

/* Equivalent to: */

typedef union {
    uint8  c[3][4];
    uint32 alignment;
} _colours_t;

static const _colours_t col_8888 =
...
Al
You beat me on it. +1
ereOn
@Al - Of course, I can't understand how I didn't spot this. I've clearly been staring at my code too long.
BeeBand