当前位置:网站首页>C语言自定义类型讲解 — 联合体
C语言自定义类型讲解 — 联合体
2022-07-24 17:29:00 【eat_sleep_play( )】
目录
前言
原创不易,还请各位靓男靓女多多三点支持,你们小小的三连对我有着大大的作用,将会鞭策着砥砺前行。
联合(共用体)
1.联合类型的定义
联合也是一种特殊的自定义类型
这种类型定义的变量也包含一系列的成员,特征是这些成员公用同一块空间(所以联合也叫共用体)
例1:
//联合类型的声明
union Un
{
char c;
int i;
};
int main()
{
//联合变量的定义
union Un un;
//计算连个变量的大小
printf("%d\n", sizeof(un));
return 0;
}
代码结果:

2.联合体的特点
联合的成员是共用同一块内存空间的,这样一个联合变量的大小,至少是最大成员的大小(因为联合至少得有能力保存最大的那个成员)。
例2:
//联合类型的声明
union Un
{
char c;
int i;
};
int main()
{
//联合变量的定义
union Un u;
//计算连个变量的大小
printf("%d\n", sizeof(u));
printf("%p\n", &u);
printf("%p\n", &(u.c));
printf("%p\n", &(u.i));
return 0;
}代码结果:


至少在第一个字节上大家重叠了,放在联合体的成员不是都有自己单独的空间,而是和大家共用同一块空间,所以叫共用体。就像是合租一样,有自己的单独的房间,也有和别人共用的房间。
3.题目练习
//方法1
#include<stdio.h>
int Cheack_sys()
{
int a = 1;
return *(char*)&a;
}
int main()
{
int ret = Cheack_sys();
if (1 == ret)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
return 0;
}//方法2 - 联合体求解
#include<stdio.h>
int Cheack_sys()
{
union un
{
int i;
char c;
}u;
u.i = 1;
return u.c;
}
int main()
{
int ret = Cheack_sys();
if (1 == ret)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
return 0;
}
代码结果:


给 i 赋值为1,一共是有4个字节,c 和 i 共用第一个字节,返回1 c 如果是1就是小端,返回是0就是大端。
4.联合体大小的计算
1.联合的大小至少是最大成员的大小。
2.当最大成员大小不是最大对齐数的整数倍的时候,就要对齐到最大对齐数的整数倍。
联合体也存在着内存对齐。
例1:
union Un1
{
char c[5];//5//对齐数是1
int i;//4//对齐数是4
};
union Un2
{
short c[4];//8//对齐数是2
int i;//4//对齐数是4
};
int main()
{
printf("%d\n", sizeof(union Un1));
printf("%d\n", sizeof(union Un2));
return 0;
}代码结果:

自定义类型中的结构体,位段,枚举和联合体到这里也就学习完了,大家多多支持。
边栏推荐
- Shardingsphere database read / write separation
- 量化框架backtrader之一文读懂Indicator指标
- EF core data filtering
- Getaverse,走向Web3的远方桥梁
- 数论整除分块讲解 例题:2021陕西省赛C
- [spoken English] 01 - Introduction to atom
- Canvas from getting started to persuading friends to give up (graphic version)
- Number theory division block explanation example: 2021 Shaanxi Race C
- Preliminary study of Oracle pl/sql
- Keyboard input operation
猜你喜欢
![[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~](/img/10/996d594a53d9a34a36079fed829f27.png)
[how to optimize her] teach you how to locate unreasonable SQL? And optimize her~~~

Method of querying comma separated strings in a field by MySQL

Two dimensional convolution -- use of torch.nn.conv2d

DF2NET三维模型部署

Ipaylinks, a cross-border payment integration service, won the 3A Asia Award of treasury
Shardingsphere database read / write separation

The latest Zhejiang construction safety officer simulation question bank and answers in 2022

Portfwd port forwarding

Open source Invoicing system, 10 minutes to complete, it is recommended to collect!

Number theory division block explanation example: 2021 Shaanxi Race C
随机推荐
2022 Yangtze River Delta industrial automation exhibition will be held in Nanjing International Exhibition Center in October
ansible自动化运维详解(五)ansible中变量的设定使用、JINJA2模板的使用以及ansible的加密控制
2022-07-21 Daily: Wu Enda wrote: how to establish projects suitable for AI career
CDN (content delivery network) content distribution network from entry to practice
Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
Pat a - correct spelling
nc 端口转发
Iftnews | Christie's launched its venture capital department, aiming at Web3 and metauniverse industries
AutoCAD - join merge command
Mobile robot (IV) four axis aircraft
什么是模糊理论,基础,流程
ufw 端口转发
启发式合并(含一般式、树上启发式合并 例题)
Class bytecode file
Scroll bar adjust brightness and contrast
[wechat official account H5] authorization
Demonstration experiment of scrollbar for adjusting image brightness
Check the actual data growth of the database
二维卷积——torch.nn.conv2d的使用
Supervisor common commands