当前位置:网站首页>C language custom type explanation - Consortium
C language custom type explanation - Consortium
2022-07-24 17:39:00 【eat_ sleep_ play( )】
Structure :http://t.csdn.cn/fs4UT
enumeration :http://t.csdn.cn/GNP51
Catalog
2. Characteristics of the consortium
4. Calculation of the size of the consortium
Preface
Originality is not easy. , Please also give me three points of support , Your little three company has a great effect on me , Will spur us to forge ahead .
union ( Shared body )
1. Definition of joint type
Federation is also a special custom type
Variables defined by this type also contain a series of members , The feature is that these members share the same space ( So union is also called community )
example 1:
// Declaration of union type
union Un
{
char c;
int i;
};
int main()
{
// The definition of joint variables
union Un un;
// Calculate the size of the variables
printf("%d\n", sizeof(un));
return 0;
}
Code results :

2. Characteristics of the consortium
Members of the union share the same memory space , The size of such a joint variable , At least the size of the largest member ( Because the Union has to be able to keep at least the largest member ).
example 2:
// Declaration of union type
union Un
{
char c;
int i;
};
int main()
{
// The definition of joint variables
union Un u;
// Calculate the size of the variables
printf("%d\n", sizeof(u));
printf("%p\n", &u);
printf("%p\n", &(u.c));
printf("%p\n", &(u.i));
return 0;
}Code results :


At least on the first byte, everyone overlaps , Not all members of the consortium have their own separate space , But share the same space with everyone , So it's called community . It's like sharing , Have your own separate room , There are also rooms shared with others .
3. Problem practice
// Method 1
#include<stdio.h>
int Cheack_sys()
{
int a = 1;
return *(char*)&a;
}
int main()
{
int ret = Cheack_sys();
if (1 == ret)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}// Method 2 - Consortium solution
#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(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}
Code results :


to i The assignment is 1, There is a total of 4 Bytes ,c and i Share the first byte , return 1 c If it is 1 It's the small end , Return is 0 It's big end .
4. Calculation of the size of the consortium
1. The size of the union is at least the size of the largest member .
2. When the maximum member size is not an integral multiple of the maximum number of alignments , It's about aligning to an integer multiple of the maximum number of alignments .
The consortium also has memory alignment .
example 1:
union Un1
{
char c[5];//5// The alignment number is 1
int i;//4// The alignment number is 4
};
union Un2
{
short c[4];//8// The alignment number is 2
int i;//4// The alignment number is 4
};
int main()
{
printf("%d\n", sizeof(union Un1));
printf("%d\n", sizeof(union Un2));
return 0;
}Code results :

Structure in custom type , Bit segment , Enumerations and consortiums will be finished here , I got a lot of support .
边栏推荐
- Niuke linked list solution record
- Logical operation of image pixels
- Reptiles and counter crawls: an endless battle
- TCP协议调试工具TcpEngine V1.3.0使用教程
- 2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
- Pat class A - check in and check out
- C语言自定义类型 — 枚举
- Opencv has its own color operation
- NATBypass 端口转发
- Is Shenwan Hongyuan securities' low commission account reliable, reliable and safe
猜你喜欢

ShardingSphere数据库读写分离

Three.js (7): local texture refresh

DF2NET三维模型部署

数论整除分块讲解 例题:2021陕西省赛C

UFW port forwarding

MySQL数据库的一个问题

C语言编程训练题目:左旋字符串中的k个字符、小乐乐与欧几里得、打印箭型图案、公务员面试、杨树矩阵

nc 端口转发

Ipaylinks, a cross-border payment integration service, won the 3A Asia Award of treasury

Number theory division block explanation example: 2021 Shaanxi Race C
随机推荐
Trends of semiconductor industry
Use yarn
快速完成intelij idea的单元测试JUnit4设置
Is computer monitoring true? Four experiments to find out
[wechat official account H5] authorization
Gan Development Series II (pggan, Singan)
ShardingSphere数据库读写分离
【网络安全】网站中间件存在的解析漏洞
nc 端口转发
Hcip day 3
通道的分离与合并
In the morning, Tencent took out 38K, which let me see the ceiling of the foundation
Use 4D nerf to display occlusion (cvpr2022)
One article of quantitative framework backtrader: understand indicator indicators
Colleges and universities have introduced 23 Philippine doctors with heavy funds, and the relevant departments have launched an investigation!
Open source Invoicing system, 10 minutes to complete, it is recommended to collect!
Pat class A - A + B format
pinia 入门及使用
2022 ranking list of database audit products - must see!
OpenCV 图片旋转