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语言·算法
爱编码的小八嘎1 天前
C语言完美演绎9-12
c语言
Navigator_Z1 天前
LeetCode //C - 1031. Maximum Sum of Two Non-Overlapping Subarrays
c语言·算法·leetcode
leoufung1 天前
LeetCode 30:Substring with Concatenation of All Words 题解(含 C 语言 uthash 实现)
c语言·leetcode·c#
爱编码的小八嘎1 天前
C语言完美演绎9-6
c语言
SunnyByte1 天前
线性表——单链表的增删查改操作
c语言·单链表
SunnyByte1 天前
线性表——双向链表
c语言·链表
jimy11 天前
C 语言的 static 关键字作用
c语言·开发语言·算法
handler012 天前
算法:图的基本概念
c语言·开发语言·c++·笔记·算法·图论
木木_王2 天前
嵌入式Linux学习 | 数据结构 (Day03)顺序表与单链表 超详细解析(含 C 语言实现 + 作业 + 避坑指南)
linux·c语言·数据结构·学习