是否有工会联盟这样的事情?(Is there such a thing as a union of unions?)

编程入门 行业动态 更新时间:2024-10-26 17:24:40
是否有工会联盟这样的事情?(Is there such a thing as a union of unions?)

我遇到了以下代码 - 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 = ...

更多推荐

本文发布于:2023-04-29 08:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工会   事情   联盟   unions   union

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!