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

边栏推荐
- Postgresql-xl全局快照代码走读与GTM原理(支线1)
- PX4模块设计之十四:Event设计
- Really touch the fish and lead the teacher: The programmer brother works 10 minutes a day with an annual salary of 570,000. I broke the defense...
- 2022暑假 动态规划总结
- 嵌入式开发:嵌入式基础——软件错误分类
- 智能合约安全——重入漏洞
- PIL库和opencv库
- 效率技巧│十分钟学会 xmind 思维导图的使用
- 第21章 内存管理
- word文档里插入图片显示不完整,只显示一半,怎么处理?
猜你喜欢

centos7 server security policy

function arguments

洪九果品、百果园抢滩港股,卖水果是门好生意吗?

搭建自己的以图搜图系统 (一):10 行代码以图搜图

MySQL 中的反斜杠 \\,我上当了

KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!

函数的参数

Mobile Banking Experience Test: How to Get the Real User Experience

error TS1219: Experimental support for decorators解决

What should I do if the Win11 network is unstable?The solution to frequent disconnection of wifi connection in Win11
随机推荐
会议OA项目之待开会议&&所有会议功能
MySQL筑基篇之增删改查
Test push | Ali Fliggy, Baidu, 58 (recruitment), Zhihu, Huanxin Network, Baiguoyuan, Ali (Lazada), Shenzhicheng, Yuanrong Qixing are recruiting
What should I do if the Win11 network is unstable?The solution to frequent disconnection of wifi connection in Win11
Chapter 01 Installation and use of MySQL under Linux [1. MySQL Architecture] [MySQL Advanced]
关于高考选志愿
Apifox免费吗?完全免费,不限团队人数,不限功能
算力顶天地,存力纳乾坤:国家超级计算济南中心的一体两面
“加价”都换不来安全保障,雷克萨斯LM之殇,到底是谁的错?
【PyCharm 常用快捷键】
Working for 9 years!
迅为i.MX8MM开发板Coatrx-M4内核开发给IAR安装8MM补丁
KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!
Apache Doris 1.1 特性揭秘:Flink 实时写入如何兼顾高吞吐和低延时
碎片化时间真的适合学习吗?
QT 如何计算中英文字符串的长度
swagger @ApiModel @ApiModelProperty注解属性说明「建议收藏」
Android 面试黑洞——当我按下 Home 键再切回来,会发生什么?
2.5w字 + 36 张图爆肝操作系统面试题 学不吐你
线程池 ThreadPoolExecutor 详解