当前位置:网站首页>C语言:联合体知识点总结
C语言:联合体知识点总结
2022-07-29 04:12:00 【白的夜gxw】
联合声明与结构声明类似,但是,联合的成员共享相同的存储空间,而且在联合中同一时间内只能有一个成员。
联合简介
联合(union)是一种数据类型,他能在同一个内存空间中存储不同的数据类型(不是同时存储)。也有人称呼他为共用体,意思是公用同一个存储空间。
创建联合
union hold{ int dight; double bigfl; char letter; }fit;根据以上形式声明的结构可以存储一个int型数据,double型数据,char型数据。但是声明的联合只能存储一个int类型的值或者是一个double类型的值亦或者是一个char类型的值。
使用联合
下面是联合的一些用法:
fit.dight = 23;//把23存储在fit,占2个字节 fit.bigfl = 2.0;//清除23,存储2.0,占8个字节 fit.letter = 'h';//清除2.0,存储h,占1个字节在联合中,一次中存储一个值。即使有足够的空间,也不能同时存储一个char类型的值和一个int类型的值。这点需要重点理解,这正是联合体和结构体最大的区别。下面的代码帮助大家理解。
#inlude <stdio.h>
//联合体模板
union hold{
int dight;
double bigfl;
char letter;
}fit;
int main()
{
fit.bigfl = 2.0; //清除23,存储2.0,占8个字节
fit.dight = 23; //把23存储在fit,占2个字节
fit.letter = 'h'; //清除2.0,存储h,占1个字节
printf("union demo:%f\n",fit.bigfl);
printf("union demo:%d\n",fit.dight);
printf("union demo:%c\n",fit.letter);
}
如果运行这段代码大家就会发现出现了奇奇怪怪的现象,虽然给每个成员都赋值,但是打印却没有打印出来,这就是联合体的特点,每次只能存储一个类型,存储下一个类型时上一个类型会被消除。(即使没有被消除也不建议同时存储俩个数据类型)。
那么联合体感觉没啥用啊,一次只能存储一个数据类型的数据,还不如使用结构体。哈哈,存在即合理。在某些场景中联合体还是很还用的。比如下面这个场景:
请你完成这个需求,要求可以存放老师的信息(姓名,年龄,性别,教的科目),还有学生的信息(姓名,年龄,性别,班级)。你肯定会心一笑,心想这可难不倒你,提笔就来。
//老师的模板
struct Teaher{
char name[20];
int old;
char sex;
char object[20];
}TEACHER;
//学生的模板
struct Student{
char name[20];
int old;
char sex;
char class[20];
}STUDENT;
大功告成,可是。。。这样会不会有点浪费空间呢?他们的结构只是最后的职业不同,其他的都一样,如果类似的模板非常多,难道我们每一个都需要创建一个吗?显然不可能。这个时候共用体就亮相了,我们来看。
struct comment{
char name[20];
int old;
char sex;
union {
char object[20];
char class[20];
};
}T;
这样便完美的解决了这个需求,顺带一提,这样的结构成为匿名联合(即没有名称的结构成员)。如果想要访问共用体的成员的话,可以使用T.object 或者 T.class来对他们进行赋值。
边栏推荐
- How to set the SQL execution timeout for flick SQL
- 有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误
- UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
- A little understanding of pointer, secondary pointer, wild pointer, pointer as function return value
- Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
- Routing knowledge
- "Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
- Mmdetection preliminary use
- Openfeign asynchronous call problem
- The function "postgis_version" cannot be found when installing PostGIS
猜你喜欢
![[原理] 横向渗透的几种方式](/img/fc/2ef7dd6ebc5c0bd8f7d302d8b596d6.png)
[原理] 横向渗透的几种方式

BGP的基础配置---建立对等体、路由宣告

初识C语言(3)

Three tier architecture of enterprise network
![[kvm] create virtual machine from kickstart file](/img/0e/292ccb6862e29d948ad6ece86b7945.png)
[kvm] create virtual machine from kickstart file

C language to achieve three chess game (detailed explanation)

Basic configuration of BGP - establish peers and route announcements

RMAN do not mark expired backups

编译与链接

Note: restframe work records many to one tables, how to serialize in that table (reverse query)
随机推荐
JS realizes the function of one click Copy
When defining an array, the size must be constant
Const read only variable constant
Is the array name a pointer
Data mining -- code implementation of association analysis example (Part 2)
MySQL Part 4 (end)
How to set the SQL execution timeout for flick SQL
Pointer of pointer???...
Is the browser multi process or single process?
Object array merges elements according to a field
【深度学习CPU(番外篇)——虚拟内存】
有一种密码学专用语言叫做ASN.1
如何查询版本的提交号
Fuzzy query of SQL
Three tier architecture of enterprise network
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation
SQL server当存储过程接收的参数是int类型时,如何做判断?
淘宝商品详情接口(商品详情页面数据接口)
The function "postgis_version" cannot be found when installing PostGIS
Change the value of the argument by address through malloc and pointer