当前位置:网站首页>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.
总结

边栏推荐
- 测试基础:Nosql数据库之Redis
- Neo4j Open Source NoSQL Database
- 牛客网刷题记录 || 指针
- R语言使用treemap包中的treemap函数可视化treemap图:treemap将分层数据显示为一组嵌套矩形,每一组都用一个矩形表示,该矩形的面积与其值成正比
- 总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对
- Canal实现Mysql数据增量同步更新至Mysql/Redis
- Security whole configuration does not take effect after the Gateway?
- 函数的参数
- 优雅实现经典的生产者消费者模式
- R语言时间序列数据提取:使用xts包的last函数提取时间序列中最后面两周的数据(last 2 week)
猜你喜欢
随机推荐
【二叉树】好叶子节点对的数量
OpenCV - 图像二值化处理 腐蚀膨胀 边缘检测 轮廓识别
Apifox免费吗?完全免费,不限团队人数,不限功能
h264和h265视频流SDP描述详解
UE4选不中半透明物体(半透明显示快捷键是啥)
R语言时间序列数据提取:使用xts包的last函数提取时间序列中最后面10天的数据(last 10 day)
The backslash \\ in MySQL is really a pit
【盘古Coder】:高性能函数级程序语言生成模型
pytorch构建YOLOV7网络结构
Typescript mix method to class with decorator
[数学]必备基本知识
updatexml、extractvalue和floor报错注入原理
手势密码解锁微信小程序项目源码
经典SQL语句大全
First-line big factory software test interview questions and answer analysis, the strongest version of 2022...
Mobile Banking Experience Test: How to Get the Real User Experience
笛卡尔树(暑假每日一题 9)
31个!Golang常用工具来啦(建议收藏)
word文档里插入图片显示不完整,只显示一半,怎么处理?
7 lines of code crashed station B for 3 hours, but because of "a tricky 0"







