当前位置:网站首页>C language advanced enumeration and joint
C language advanced enumeration and joint
2022-07-29 20:03:00 【Shark-s】
前言
Today we will introduce you to the enumeration and complex this two concepts,Both we and the combined structure comparative study,All three have their similarities between,Of course, there are a lot of difference between,Today we will be from the basic definition of enumeration and complex,Analysis of the use and function of both.
一、枚举
Let's speak enumeration,从字面意义上来说,枚举的意思就是一一列举
That list all possible values
比如说:In January, a total of31天,这是有限的,We can list
又如:Gender only three of the following,男、Female or confidential,We can also list
等等…
诸如此类,We can call the list
In this case, we can use the enumeration
1.1 枚举类型的定义
先来看几个例子:
enum Day //星期
{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
enum Sex //性别
{
MALE,
FEMALE,
SECRET
};
以上定义的 enum Day,enum Sex都是枚举类型.
{}中的内容是枚举类型的可能取值,也叫枚举常量 .
If we want to describe a week now how should express with code?我们可以这样做:
int main()
{
enum Day d = Fri;
return 0;
}
以此类推,The same is true in other enum type
Here one could ask for,Since the content is called enumerated constants within the curly braces,Then enumerated constants have value?
Here I can clearly tell you,这些枚举常量都是有值的,大家可以尝试用printfFunction will print out all of the value of an enum constants than the,
We can easily find an interesting rule:We enumerate the possible value is from0开始的,依次往下递增1,Of course, here is the premise of default.
So if we can modify their values?For example, I hope the first enumerated constants from1开始,It is easy to implement,只需将"Mon"改为"Mon = 1"即可.
At the back of the enum constants also as well as by default, in turn, increasing1.
1.2 枚举的优点
为什么使用枚举?What are the benefits of using the enumeration?
首先我们知道,除了使用枚举,我们还可以使用#define定义常量,如:
#define Mon 1
#define Tues 2
//....
Now that we can usedefine来定义一个常量,That compared with it,Enumeration advantage in where?
Let's take a look at the enumeration of the advantages:
增加代码的可读性和可维护性.
和#define定义的标识符比较枚举有类型检查,更加严谨.
防止了命名污染
便于调试
使用方便,一次可以定义多个常量
1.3 枚举的使用
Look at a simple example and you will understand:
enum Color //颜色
{
RED = 1,
GREEN = 2,
BLUE = 4,
};
enum Color clr = GREEN; //Can only take enumerated constants enumeration variable assignment,才不会出现类型的差异
clr = 5;
二、联合体(共用体)
2.1联合类型的定义
联合也是一种特殊的自定义类型
这种类型定义的变量也包含一系列成员,特征是这些成员公用同一块空间(所以联合体也叫共用体)
比如:
union Un
{
int a;
char c;
};
So what do we want to understand the common?We can for the following operation:
int main
{
union Un u;
printf("%d\n", sizeof(u));
return 0;
}
According to our usual habits of thinking,一个整型+A character also have to take up at least5个字节
But when we execute a program found,只占用了4个字节,This is to give full expression to the Shared this one characteristic.
We can take a look at the two address:
printf("%p\n", &u);
printf("%p\n", &(u.a));
printf("%p\n", &(u.b));
Execute after we can find that,The three address is the same,How do we explain?
Actually very simple to explain:
We draw the first four bytes of space,黄色部分就是a所占用的空间,And the brown part isb所占用的空间,He covered thea的一部分空间,But is the coexistence of,Does not have exclusive phenomenon,It and common name is also very in tune.
2.2 联合的特点
联合的成员是共用同一块内存空间的,这样一个联合变量的大小,至少是最大成员的大小(Because the association at least have the ability to save the members of the biggest).
We in the above mentioned the concept of sharing.That someone will ask questions,The address is the same,When you for one of the variables to change ,Another variable in memory will also be changed,This will create problems.
正是因为有这样的问题,所以我们要在实际使用时,我们在同一时间, Use only one variable,In one of the variables don't have another,这样就可以避免这个问题.
2.3 联合大小的计算
- 联合的大小至少是最大成员的大小.
- 当最大成员大小不是最大对齐数的整数倍的时候,就要对齐到最大对齐数的整数倍.
来看一个例子:
union Un1
{
char c[5];
int i;
};
union Un2
{
short c[7];
int i;
};
//下面输出的结果是什么?
printf("%d\n", sizeof(union Un1));
printf("%d\n", sizeof(union Un2));
Executable program after we found,结果分别是8和16.这是为什么呢?
对于un1,当有数组的时候,最大对齐数为数组成员,un1中为char ,对齐数就是1,int 为4是un1中最大对齐数,故结果为8.
un2同理,short为2,数组共14,但是最大对齐数为4,整数倍故为16.
总结

边栏推荐
猜你喜欢
随机推荐
593. 有效的正方形 改善丑陋的代码
我用两行代码实现了一个数据库!
What should I do if the Win11 network is unstable?The solution to frequent disconnection of wifi connection in Win11
leetcode:36. 有效的数独
小程序组件的总结
R语言时间序列数据提取:使用xts包的first函数提取时间序列中最前面两周的数据(first 2 week)
AI 通过了图灵测试,科学家反应冷淡:“很棒,但没必要”
cv2 imread()函数[通俗易懂]
自定义组件-behaviors
R语言时间序列数据提取:使用xts包的first函数提取时间序列中最前面一个月的数据(first 1 month)
PX4模块设计之十四:Event设计
无人驾驶技术有什么优点,人工驾驶的优缺点英文
小程序使用npm包
centos7 server security policy
线程池 ThreadPoolExecutor 详解
在宇宙中心五道口上班,是怎样一种体验
Gesture password unlock WeChat applet project source code
知识库对企业的意义
7 lines of code crashed station B for 3 hours, but because of "a tricky 0"
函数的参数








