当前位置:网站首页>C language: summary of consortium knowledge points
C language: summary of consortium knowledge points
2022-07-29 04:16:00 【White night gxw】
The joint declaration is similar to the structure declaration , however , Members of the Federation share the same storage space , And there can only be one member in the union at a time .
Joint profile
union (union) It's a data type , It can store different data types in the same memory space ( Not stored at the same time ). Some people call him the common body , It means sharing the same storage space .
Create a Federation
union hold{ int dight; double bigfl; char letter; }fit;The structure declared according to the above form can store a int Type data ,double Type data ,char Type data . But the declared union can only store one int Value of type or a double The value of type or a char Type value .
Use Union
Here are some uses of Union :
fit.dight = 23;// hold 23 Stored in fit, Occupy 2 Bytes fit.bigfl = 2.0;// eliminate 23, Storage 2.0, Occupy 8 Bytes fit.letter = 'h';// eliminate 2.0, Storage h, Occupy 1 BytesIn the Union , Store one value at a time . Even if there's enough space , You cannot store one at the same time char Value of type and one int Type value . This point needs to be understood , This is the biggest difference between a consortium and a structure . The following code helps you understand .
#inlude <stdio.h>
// Consortium template
union hold{
int dight;
double bigfl;
char letter;
}fit;
int main()
{
fit.bigfl = 2.0; // eliminate 23, Storage 2.0, Occupy 8 Bytes
fit.dight = 23; // hold 23 Stored in fit, Occupy 2 Bytes
fit.letter = 'h'; // eliminate 2.0, Storage h, Occupy 1 Bytes
printf("union demo:%f\n",fit.bigfl);
printf("union demo:%d\n",fit.dight);
printf("union demo:%c\n",fit.letter);
}
If you run this code, you will find strange phenomena , Although each member is assigned a value , But the print did not print out , This is the characteristic of the consortium , Only one type can be stored at a time , When the next type is stored, the previous type is eliminated .( Even if it is not eliminated, it is not recommended to store two data types at the same time ).
Then the consortium feels useless , Only one data type can be stored at a time , It's better to use structure . ha-ha , Existence is reason . In some scenarios, the consortium is still very useful . For example, the following scene :
Please complete this requirement , It is required that teachers' information can be stored ( full name , Age , Gender , Subjects taught ), And students' information ( full name , Age , Gender , class ). You must have a knowing smile , I thought it was not difficult for you , Just pick up the pen .
// Teacher's template
struct Teaher{
char name[20];
int old;
char sex;
char object[20];
}TEACHER;
// Student templates
struct Student{
char name[20];
int old;
char sex;
char class[20];
}STUDENT;
Be accomplished , But ... Is this a bit of a waste of space ? Their structure is just different in the final profession , Everything else is the same , If there are many similar templates , Do we each need to create one ? Obviously not . At this time, the community appeared , Let's see .
struct comment{
char name[20];
int old;
char sex;
union {
char object[20];
char class[20];
};
}T;
This perfectly solves this need , By the way , Such a structure becomes anonymous Association ( That is, structure members without names ). If you want to access the members of the community , have access to T.object perhaps T.class To assign values to them .
边栏推荐
- flink-sql 如何设置 sql执行超时时间
- The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
- Why do I delete the original record (OP d) and then add a new one in Kafka when I update MySQL data
- Deep learning training strategy -- warming up the learning rate
- “蔚来杯“2022牛客暑期多校训练营1 J Serval and Essay(启发式合并)
- 开课!看smardaten如何分解复杂业务场景
- 优炫数据库有办法查到主集群每天传给备集群的日志量吗?
- 12.优先级队列和惰性队列
- C语言力扣第61题之旋转链表。双端队列与构造循环链表
- Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
猜你喜欢

It won't last for 65 days. It only appears once

Installation and use of stm32cubemx (5.3.0)

Won't you just stick to 62 days? Sum of words

The principle of inverse Fourier transform (IFFT) in signal processing

Applet: Area scrolling, pull-down refresh, pull-up load more

Some problems about pointers

Problems encountered in vscode connection SSH

小程序:区域滚动、下拉刷新、上拉加载更多

Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes

全屋WiFi方案:Mesh路由器组网和AC+AP
随机推荐
Semantic segmentation correlation
[Openstack] keystone,nova
Won't you just stick to 69 days? Merge range
The return value of the function is the attention of the pointer, the local variables inside the static limit sub function, and how the pointer to the array represents the array elements
Do you have a boss to help me check whether the parameter configuration of the Flink SQL connection Kafka authentication Kerberos is wrong
kotlin的List,Map,Set等集合类不指定类型
Multi rotor six axis hardware selection
不会就坚持60天吧 神奇的字典
小程序:区域滚动、下拉刷新、上拉加载更多
Opengauss pre check installation
Introduction and examples of parameters in Jenkins parametric construction
When array is used as a function parameter, it is better to use the array size as a function parameter
It won't last for 65 days. It only appears once
9.延迟队列
What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses
伏英娜:元宇宙就是新一代互联网!
Object detection: object_ Detection API +ssd target detection model
Record of problems encountered in ROS learning
编译与链接
[material delivery UAV] record (ROS + Px4 + yolov5 + esp8266 + steering gear)