union用法

文章目录


union

union有很多成员,在使用时只对其中的一个成员生效。

这样可以省空间。

联合体总大小 = 最大成员大小 = 8

关于字节对齐,可以填充无用字节或者用汇编#pragma pack来指定下来的声明用几字节对齐。

c 复制代码
#pragma pack(push, 1)      // 把当前对齐值压栈,并设为 1 字节对齐
typedef struct __attribute__((packed)) {
    uint8_t  a;
    uint16_t b;  
    uint32_t c;
} Msg_t;
#pragma pack(pop)          // 恢复成 push 之前的对齐值(通常是 4/8)

比如下列的就是union的省空间用法,这样这一个8字节的union可以用来表示非常多不同的结构体。

c 复制代码
union dev_dev_io_cmd_resp
{
    uint8_t rev[8];
    struct dev_dev_dido_cmd0_resp0
    {
        // bit 0 - 6: tsdev_dev_dido_set_glb_par
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t is_12v;
        uint16_t report_cycle_ms; // tsdev_dev_dido_io_report report cycle
    } cmd0_resp0;
    struct dev_dev_dido_cmd1_resp1
    {
        // bit 0 - 6: tsdev_dev_dido_set_bridge
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t dido_1_2; // dido1 bridge dido2,0: reset; 1: set; 2: not change,
        uint8_t dido_3_4; // dido3 bridge dido4,0: reset; 1: set; 2: not change,
    } cmd1_resp1;
    struct dev_dev_dido_cmd2_resp2
    {
        // bit 0 - 6: tsdev_dev_dido_mode
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t dido_idx;
        uint16_t dido_input_thread;   // 0-5000, unit is 1mV
        uint8_t dido_mode0;           // bit0: io type: 0: io; 1: pwm
        uint8_t dido_mode1;           // bit0-1: 0: input; 1: push-pull; 2: pull(p-mos) only; 3: push(n-mos) only /// ? out high/low level
        uint16_t pwm_report_cycle_ms; // tsdev_dev_dido_pwm_report report cycle
    } cmd2_resp2;
    struct dev_dev_aiao_cmd3_resp3
    {
        // bit 0 - 6: tsdev_dev_aiao_mode
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t aiao_idx;
        uint8_t aiao_mode; // 0: input, 1: output
        uint8_t rev;
        uint16_t report_cycle_ms;
    } cmd3_resp3;

    struct dev_dev_dido_cmd4_resp4
    {
        // bit 0 - 6: tsdev_dev_dido_read/tsdev_dev_dido_write
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t dido_idx;
        uint8_t io_status;
        uint8_t rev;
    } cmd4_resp4;
    struct dev_dev_dido_cmd5_resp5
    {
        // bit 0 - 6: tsdev_dev_pwm_read/tsdev_dev_pwm_write
        // bit 7: op_valid_ok (cmd: cmd valid;  resp: op ok)
        uint8_t cmd;
        uint8_t pwm_idx;
        uint16_t duty; // uint is 0.01%
        uint32_t freq; // unit is hz
    } cmd5_resp5;
};
相关推荐
祈安_1 天前
C语言内存函数
c语言·后端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
czy87874753 天前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
m0_531237173 天前
C语言-数组练习进阶
c语言·开发语言·算法
Z9fish3 天前
sse哈工大C语言编程练习23
c语言·数据结构·算法
代码无bug抓狂人3 天前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
CodeJourney_J3 天前
从“Hello World“ 开始 C++
c语言·c++·学习
枫叶丹43 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
with-the-flow3 天前
从数学底层的底层原理来讲 random 的函数是怎么实现的
c语言·python·算法
Sunsets_Red3 天前
P8277 [USACO22OPEN] Up Down Subsequence P 题解
c语言·c++·算法·c#·学习方法·洛谷·信息学竞赛