共用体union

一、共用体的特性

共用体又叫做联合体,共用体的特性如下:

1.共用体的所有成员共用一段内存空间,且所有成员的起始位置是一致的

2.共用体的值由最后赋值的成员决定

3.共用体的内存大小

  • 共用体的内存必须大于或等于 其他成员变量中最大数据类型(含数组)的大小。
  • 共用体的内存必须是最宽**++基本数据类型++** 的整数倍,如果不是,则填充字节。

二、共用体的用途1(判断大小端)

cpp 复制代码
#include <stdio.h>
union myunion {
    int a;
    char b;
};
int main()
{
    union myunion test;
    test.a = 0x12345678;        
    if (test.b == 0x78)
        printf("小端模式");
    else if (test.b == 0x12)
        printf("大端模式");    
     return 0;
}

三、共用体的用途2(给GPIO赋值)

cpp 复制代码
/* 用途2: 给成员集体赋值*/
#include <stdio.h>

struct GPIO{
	unsigned short Pin0 :1;
	unsigned short Pin1 :1;
	unsigned short Pin2 :1;
	unsigned short Pin3 :1;
	unsigned short Pin4 :1;
	unsigned short Pin5 :1;
	unsigned short Pin6 :1;
	unsigned short Pin7 :1;
	unsigned short Pin8 :1;
	unsigned short Pin9 :1;
	unsigned short Pin10 :1;
	unsigned short Pin11 :1;
	unsigned short Pin12 :1;
	unsigned short Pin13 :1;
	unsigned short Pin14 :1;
	unsigned short Pin15 :1;
};

union IODevice{
	unsigned short gpiovalue;
	struct GPIO gpiox;
};

int main()
{
	union IODevice IO;
	IO.gpiovalue=0xff;
	printf("%d\n",IO.gpiox.Pin0);
}

四、共用体的用途3

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

#define TOTAL 4  //人员总数

struct{
    char name[20];
    int num;
    char sex;
    char profession;
    union{
        float score;
        char course[20];
    } sc;
} bodys[TOTAL];

int main(){
    int i;
    //输入人员信息
    for(i=0; i<TOTAL; i++){
        printf("Input info: ");
        scanf("%s %d %c %c", bodys[i].name, &(bodys[i].num), &(bodys[i].sex), &(bodys[i].profession));
        if(bodys[i].profession == 's'){  //如果是学生
            scanf("%f", &bodys[i].sc.score);
        }else{  //如果是老师
            scanf("%s", bodys[i].sc.course);
        }
        fflush(stdin);
    }

    //输出人员信息
    printf("\nName\t\tNum\tSex\tProfession\tScore / Course\n");
    for(i=0; i<TOTAL; i++){
        if(bodys[i].profession == 's'){  //如果是学生
            printf("%s\t%d\t%c\t%c\t\t%f\n", bodys[i].name, bodys[i].num, bodys[i].sex, bodys[i].profession, bodys[i].sc.score);
        }else{  //如果是老师
            printf("%s\t%d\t%c\t%c\t\t%s\n", bodys[i].name, bodys[i].num, bodys[i].sex, bodys[i].profession, bodys[i].sc.course);
        }
    }
    return 0;
}

五、共用体的用途4(类型转换)

cpp 复制代码
union Converter {
    int i;
    float f;
};

// 使用共用体进行类型转换
union Converter conv;
conv.f = 3.14;
int intValue = conv.i;

六、共用体的用途5(处理特定硬件寄存器)

cpp 复制代码
union Register {
    unsigned int reg;
    struct {
        unsigned int bit0: 1;
        unsigned int bit1: 1;
        unsigned int bit2: 1;
        unsigned int bit3: 1;
        unsigned int reserved: 28;
    } bits;
};
相关推荐
L_09072 分钟前
【Algorithm】Day-4
c++·算法·leetcode
代码充电宝8 分钟前
LeetCode 算法题【简单】20. 有效的括号
java·算法·leetcode·面试·职场和发展
海琴烟Sunshine10 分钟前
leetcode 119. 杨辉三角 II python
算法·leetcode·职场和发展
小杨的全栈之路10 分钟前
霍夫曼编码:数据压缩的核心算法详解(附图解 + 代码)
算法
cjinhuo24 分钟前
标签页、书签太多找不到?AI 分组 + 拼音模糊搜索,开源插件秒解切换难题!
前端·算法·开源
贝塔实验室27 分钟前
频偏估计方法--快速傅里叶变换(FFT)估计法
网络协议·算法·数学建模·动态规划·信息与通信·信号处理·傅立叶分析
闭着眼睛学算法1 小时前
【双机位A卷】华为OD笔试之【模拟】双机位A-新学校选址【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
玉夏1 小时前
【每日算法C#】爬楼梯问题 LeetCode
算法·leetcode·c#
学好statistics和DS1 小时前
【CV】泊松图像融合
算法·计算机视觉
贝塔实验室1 小时前
QPSK信号载波同步技术---极性Costas 法载波同步
计算机网络·算法·网络安全·数学建模·信息与通信·信号处理·傅立叶分析