当前位置:网站首页>Type explanation (enumeration + Union)
Type explanation (enumeration + Union)
2022-07-20 11:07:00 【Qingfeng jade bone ( ω・)「 well】
Catalog
Definition of enumeration type
enumeration
Is to enumerate objects , For example, Monday to Sunday can be listed one by one in a week
Definition of enumeration type
For example, the definitions of the following three enumerations
// weekenum Day{Mon,Tues,Wed,Thur,Fri,Sat,Sun};// Genderenum Sex{MALE,FEMALE,};// Colorenum Color{RED,GREEN,BLUE};
As defined above enum Day , enum Sex , enum Color All enumeration types .{ } The contents in are possible values of enumeration types , Also called Enumeration constants .All these possible values have values , The default from the 0 Start , One increment 1, Of course, initial values can also be assigned when defining .
Of course, we can also change their values
Advantages of enumeration
Of course, we can also use #define Define constantsAdvantages of enumeration :1. Increase the readability and maintainability of the code2. and #define The defined identifier comparison enumeration has type checking , More rigorous .3. Prevent named pollution ( encapsulation )4. Easy to debug5. Easy to use , You can define more than one constant at a time
matters needing attention
Enumerations are typed , It is important to note this , Some codes are in C Can be run in, but in, for example, the syntax is more strict C++ There will be errors on it
For example, in C in , This is allowed
#include<stdio.h>
enum Day
{
Mon = 1, // Enumeration constants
Tues,
};int main() {
enum Day S = 5; // stay C You can write like this in , But in the more strict grammar C++ Is not like
return 0; // Because enumeration constants cannot be modified
}
stay C++ An error will be reported directly in , Because the type cannot be modified
union ( Shared body )
Definition of joint type
Federation is also a special custom typeVariables 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 ).
// Consortium
#include<stdio.h>
// Declaration of union type
union Un
{
char c;
int i;
};
int main()
{
union Un u;
// Calculate the size of the variable
printf("%d\n", sizeof(u));
printf("%p\n", &u);
printf("%p\n", &(u.c));
printf("%p\n", &(u.i));
return 0;
}
We will find such a unique phenomenon
Characteristics of Union
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 )
Using the characteristics of the consortium can , Find out the size of the current computer
// Get the problem of the size end of the current machine
#include<stdio.h>
int Check_sys()
{
// Law 1 : Use the characteristics of type to get the size end
//int i = 1;
//return *(char*)&i;
// Law two : Use the characteristics of the consortium to get the big and small end
union Un
{
int i;
char c;
}u;
u.i = 1;
return u.i;
}
int main() {
int ret = Check_sys();
if (ret = 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}
Calculation of joint size
Looking at the following lines of code, we can see that the union is actually aligned with the structure
The size of the union is at least the size of the largest member .
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 .
Conclusion
as time goes on , Increase in the amount of code , You can gradually understand more code in your eyes , It's really a wonderful realm , It's like upgrading a monster , Crooked
边栏推荐
猜你喜欢
随机推荐
10day
【电子器件笔记2】电阻的使用技巧
5day
[yunyuansheng boy rushes into the Ninth Heaven of IVX platform] 1: project creation of actual ivx-0 code programming experience
如何对CDH集群中的Impala打印线程堆栈
17day
20day
什么是「中华田园敏捷开发」,人才
18day
【云原生小子怒闯iVX平台九重天】1:实战iVX-0代码编程体验之项目创建
Impala查询卡顿分析案例
【英雄哥七月集训】第 18天:树
11day
Location information of ORC index
17day
GC tuning principle of JVM (7)
盒模型再回顾:外边距折叠原理与BFC的关系
Using SQL injection vulnerability to practice reading and writing files
Apache impala 4.0 overview
Flink内核源码(六)状态容错与两阶段提交