当前位置:网站首页>C Language Basics_Union
C Language Basics_Union
2022-08-02 08:37:00 【*fzfw】
1共用体类型
The memory space occupied by the union is the memory size of the variable that occupies the largest space among the defined union variables.
learned variables,我们知道,定义一个变量,就是在内存中,Allocate an area to store the variable's data.例如:
char a;
int b;
此时,定义了char类型的变量a,那么,char类型占据1个字节的存储空间.所以,就在内存中分配1个字节的内存,用来存储变量a的数据.同理,定义了int类型的变量b,那么,int类型占据4个字节的存储空间,所以,就在内存中分配4个字节的内存块,用来存储变量b的数据.
上面定义的变量a和变量b是两个独立的变量,Their memory spaces are separate and independent.
但是,Can we put variablesa和变量bstored in the same memory space?Just let them use the same memory space?如下图:

可以看到,在内存空间中,有0x80001这样的地址.变量a和变量b的数据都存储在0x80001in the memory space starting at this address.
那么,在C语言中,提供了“共用体类型”的定义,It can define multiple variables,使用同一个内存空间.Union types are defined as follows:
union 共用体类型名
{
成员列表;
};
可以看到,Defining a union type is very similar to defining a struct type,区别就是:定义结构体类型,使用struct关键字;定义共用体类型,使用union关键字.假设有如下的定义:
union test
{
char a;
int b;
};
此时,A union type is defined,名称是test.那么,You can use the union type to define variables,如下:
union test t;
此时,A Union type object is definedt,对象t有2个成员,分别是char类型的变量a和int类型的变量b.但是,These two members use a common memory space.如下图:

可以看到,成员变量a和成员变量bBoth use the same memory space,所以,When assigning a value to a member variable,It is assigned to the shared memory space,那么,Other member variables can refer to this value.The following is an example of a program test:

可以看到,A union type is definedunion test,然后,Use this union type to define variables:
union test t;
此时,变量tAll members in the use a common memory space.那么,给t.bmember assignment,It is to set the data to the union variabletin the shared memory block.当获取t.amember data,It is obtained from the shared memory block.所以,All members of a union variable use the same block“共用”内存空间.
Union type definition and usage
Union types are defined and used in much the same way as structs,例如,This can be done when defining the union type,定义变量,如下:
union test
{
char a;
int b;
}x, y, z;
此时,定义了union test共用体类型,同时,A union type variable is definedx、y、z.很多时候,We are separating the definition of the union type from the definition of the variable,如下:
union FreqData//定义共用体类型;
{
float Float;
u8 Byte[4];
};
union FreqData VcoFreq[2],VcoFreq1,VcoFreq2,MidVcoFreq; //定义共用体变量;
此时,We first define the union typeunionFreqData,然后,Then use the union type to define the variable VcoFreq[2],VcoFreq1,VcoFreq2,MidVcoFreq.
使用示例:
Divide the data into assignments:
Module.VcoFreq[0].Byte[0] = Module.MidVcoFreq.Byte[0];
Module.VcoFreq[0].Byte[1] = Module.MidVcoFreq.Byte[1];
Module.VcoFreq[0].Byte[2] = Module.MidVcoFreq.Byte[2];
Module.VcoFreq[0].Byte[3] = Module.MidVcoFreq.Byte[3];
调用float整体使用:
fVcoConfig(0, Module.VcoFreq[0].Float, Module.Offset[UL_FREQ_OFFSET]); //vco的更新
在定义结构体的时候,We can omit the name of the struct type,例如:
struct
{
int i;
char j;
}a, b, c;
此时,定义了 struct 结构体类型,同时,定义了3个结构体变量.那么,Because when defining the structure type,The name of the struct type was not given,所以,在代码中,Variables of this structure type can no longer be defined.
同样的道理,For the Commonwealth,也可以这样定义,如下:
union
{
char a;
int b;
}x, y, z;
可以看到,when defining the community,The union variable is definedx、y、z;但是,The name of the union type is not given,所以,It is no longer possible to define a variable of the union type.
The definition of a union type is very similar to the definition of a struct type,所以,Using union member variables is the same as struct member variables,例如:
union test
{
char a;
int b;
}x, y, z;
此时,The member variables of the reference union are as follows:
x.a --- 引用共用体变量x的成员变量a;
x.b --- 引用共用体变量x的成员变量b;
z.b --- 引用共用体变量z的成员变量b;
同时,It is also possible to define pointer variables of union type,Member variables are referenced by pointers.例如:
union test t;
union test* p; //Define a union pointer variablep;
p = &t; //指针p指向对象t;
p->a; //Via a union pointer variablepto refer to its member variablesa;
如下是一个程序测试例子:

可以看到,The union type is definedunion test,Use it to define a union type variabletand common type pointer variablesp.指针变量p指向变量t.
然后,There are members of the access community:
t.a --- through the union variablet,访问成员变量a;
p->a --- Via a union pointer variablep,访问成员变量a;
可以看到,The use of union types is very similar to the use of struct types.
When defining a union type variable,可以对其进行初始化,例如:
union test
{
char a;
int b;
};
union test t = {9};
注意:All members of the union share one memory space,所有,When initializing assignments to its members,There can only be one value,in the initialization parameter list,The parameter list can only have one value.所以,有:
union test t = {9};
此时,把数值9Stored in a union variabletin the shared memory block.成员变量a和bAll use the shared memory space.
边栏推荐
猜你喜欢

OneNote Tutorial, How to Create More Spaces in OneNote?
![[OC学习笔记]weak的实现原理](/img/39/d6183deda2a530b78a0883e0f60153.png)
[OC学习笔记]weak的实现原理

多版本node的安装与切换详细操作

血气方刚的年轻小伙竟去做家政小哥,是怎样成功逆袭转行的

Wang Xuegang - compiled shipment line file

Biotin hydrazide HCl|CAS:66640-86-6|Biotin-hydrazide hydrochloride

MySQL之创建表的基本操作

mysql 中 in 的用法

cas: 139504-50-0 Maytansine DM1|Mertansine|

redis-desktop-manager下载安装
随机推荐
C语言_条件编译
科技云报道:实现元宇宙,英伟达从打造基础建设平台开始
Biotin-C6-amine|N-生物素基-1,6-己二胺|CAS:65953-56-2
How Engineers Treat Open Source --- A veteran engineer's heartfelt words
prometheus监控mysql_galera集群
etcd实现大规模服务治理应用实战
MySQL压缩包方式安装,傻瓜式教学
IO process thread -> process -> day4
Flink 监控指南 被动拉取 Rest API
ip地址那点事(二)
OneNote Tutorial, How to Create More Spaces in OneNote?
17、生成长图,并上传至服务器
离线部署通过tiup 配置好topology.yaml文件指定PD TV TIDBserver 是不是会自动在其他机器创建好对应得模块?
PyQt5(一) PyQt5安装及配置,从文件夹读取图片并显示,模拟生成素描图像
QT web development - Notes - 3
Axial Turbine Privacy Policy
C语言基础_结构体
Shell becomes canonical and variable
三维体尺测量
Redisson distributed lock source code analysis for high-level use of redis