我遇到了以下代码 - col_8888的数据类型是什么?为什么它引用了union _colours ? 我用col_8888搜索了工会,但我找不到这种宣言的参考 - 我觉得col_8888是一个“工会联盟”?
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])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])最满意答案
col_8888的类型是union _colours ,因此它不是联合的联合:它只是一个联合。 在C中,必须在union名称前加上union来使用它。 或者,您可以使用typedef。 因此,以下两个声明是等效的:
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 = ...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 = ...更多推荐
发布评论