当前位置:网站首页>C语言自定义类型的介绍(结构体,枚举,联合体,位段)
C语言自定义类型的介绍(结构体,枚举,联合体,位段)
2022-06-24 03:54:00 【InfoQ】
1.结构体
1.1结构体概述
1.1.1结构体概念
1.1.2 结构体的声明与使用
//结构体
struct Stu
{
char name[20];//姓名
char sex[5];//性别
char id[20];//学号
int age;//年龄
double grade;//成绩
}student;
struct Stu结构体成员student//匿名结构体类型
struct
{
int a;
char b;
float c;
}x;
struct
{
int a;
char b;
float c;
}a[20], *p;
pxpstruct Node
{
int data;
struct Node next;
};
struct Node
{
int data;
struct Node* next;
};
typedeftypedef//错误示例:
typedef struct NODE
{
int data;
Node* next;
}Node;
//正确示例:
typedef struct NODE
{
int data;
struct NODE* next;
}Node;
#include <stdio.h>
int main()
{
struct Stu s = { "张三","男","20210618",20,99 };//结构体声明与初始化
printf("姓名:%s\n", s.name);//结构体成员访问方式1:结构体变量名.结构体成员名
printf("性别:%s\n", s.sex);
struct Stu* ps = &s;
printf("学号:%s\n", ps->id);//结构体访问成员方式2:结构体指针->结构体成员名
printf("年龄:%d\n", ps->age);
printf("C语言考试成绩:%.2lf\n", ps->grade);
return 0;
}
姓名:张三
性别:男
学号:20210618
年龄:20
C语言考试成绩:99.00
D:\gtee\C-learning-code-and-project\test_922\Debug\test_922.exe (进程 26272)已退出,代码为 0。
按任意键关闭此窗口. . .
1.2结构体对齐及其大小计算
1.2.1偏移量

1.2.2结构体大小计算
81#pragma pack(4)//设置默认对齐数为4
#pragma pack()//无参数表示默认值为8(vs编译器)
- 平台原因(移植原因):不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特定类型的数据,否则抛出硬件异常。
- 性能原因:数据结构(尤其是栈)应该尽可能地在自然边界上对齐。原因在于为了访问未对齐的内存,处理器需要作两次内存访问;而对齐的内存访问仅需要一次访问。
- [ ] 结构体的首成员与结构体的偏移量为
0。
- [ ] 结构体成员需要对齐到对齐数的整数倍地址(偏移量)处。
- [ ] 每个结构体成员都有一个对齐数,对齐数为编译器默认值与结构体成员大小中的较小值。
- [ ] 结构体的总大小为最大对齐数(结构体各成员对齐数中最大的一个对齐数)的整数倍。
- [ ] 如果结构体中存在嵌套结构体,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍。
//练习1
struct S1
{
char c1;
int i;
char c2;
};
//练习2
struct S2
{
char c1;
char c2;
int i;
};
//练习3
struct S3
{
double d;
char c;
int i;
};
//练习4-结构体嵌套问题
struct S4
{
char c1;
struct S3 s3;
double d;
};
char c1101
int i444
char c21181c28
c294412charc1c2111c2c1424488
double88char19int44121616
S3816char11S3168S38824double88243232
int main()
{
printf("%d\n", sizeof(struct S1));
printf("%d\n", sizeof(struct S2));
printf("%d\n", sizeof(struct S3));
printf("%d\n", sizeof(struct S4));
return 0;
}
12
8
16
32
D:\gtee\C-learning-code-and-project\test_922\Debug\test_922.exe (进程 34308)已退出,代码为 0。
按任意键关闭此窗口. . .
1.3结构体与位段
1.3.1位段
1.3.2位段实现结构体
struct A {
int _a:2;
int _b:5;
int _c:10;
int _d:30;
};
168
D:\gtee\C-learning-code-and-project\test_922\Debug\test_922.exe (进程 22660)已退出,代码为 0。
按任意键关闭此窗口. . .
816432_a230_b525_c1015_d30432_d88- 位段的成员可以是
int unsigned intsigned int或者是char(属于整形家族)类型
- 位段的空间上是按照需要以4个字节(
int)或者1个字节(char)的方式来开辟的。
- 位段涉及很多不确定因素,位段是不跨平台的,注重可移植的程序应该避免使用位段。


int位段被当成有符号数还是无符号数是不确定的。
- 位段中最大位的数目不能确定。(16位机器最大16,32位机器最大32,写成27,在16位机器会出问题。
- 位段中的成员在内存中从左向右分配,还是从右向左分配标准尚未定义。
- 当一个结构包含两个位段,第二个位段成员比较大,无法容纳于第一个位段剩余的位时,是舍弃剩余的位还是利用,这是不确定的。

2.枚举
2.1枚举概述
2.1.1枚举概念
2.1.2枚举的声明与使用
enumenum Day//星期
{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
enum Sex//性别
{
MALE,
FEMALE,
SECRET
};
enum Color//颜色
{
RED,
GREEN,
BLUE
};
枚举常量enum Color//颜色
{
RED=1,
GREEN=2,
BLUE=4
};
enum Color//颜色
{
RED=1,
GREEN=2,
BLUE=4
};
enum Color clr = GREEN;//只能拿枚举常量给枚举变量赋值,才不会出现类型的差异。
clr = 5;//错误,枚举常量不可被赋值
2.2枚举大小计算
4enum A
{
QSW,
BSW,
CWS
}a;
int main()
{
printf("%d\n", sizeof(a));
return 0;
}
4
D:\gtee\C-learning-code-and-project\test_922\Debug\test_922.exe (进程 21156)已退出,代码为 0。
按任意键关闭此窗口. . .
2.3枚举与宏的区别
#define#define - 增加代码的可读性和可维护性
- 和
#define定义的标识符比较枚举有类型检查,更加严谨。
- 防止了命名污染(封装)。
- 便于调试。
- 使用方便,一次可以定义多个常量。
3.联合体
3.1联合体概述
3.1.1联合体概念
3.1.2联合体的声明与使用
union//联合类型的声明
union Un
{
char c;
int i;
};
//联合变量的定义
union Un un;
int is_bl()
{
union BL
{
char a;
int b;
}un;
un.b = 1;
//返回1为小端,返回0位大端
if (un.a == 1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int ret = is_bl();
if (ret == 1)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
return 0;
}
小端
D:\gtee\C-learning-code-and-project\test_922\Debug\test_922.exe (进程 23328)已退出,代码为 0。
按任意键关闭此窗口. . .
3.2联合体大小计算
- [ ] 联合的大小至少是最大成员的大小。
- [ ] 当最大成员大小不是最大对齐数的整数倍的时候,就要对齐到最大对齐数的整数倍。
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));
Un15char1int448Un214short2int44168
16
D:\gtee\C-learning-code-and-project\test_922\Debug\test_922.exe (进程 12864)已退出,代码为 0。
按任意键关闭此窗口. . .
边栏推荐
- Live broadcast Reservation: Micro build practice - quickly build a catering reservation applet
- How can the new generation of HTAP databases be reshaped in the cloud? Tidb V6 online conference will be announced soon!
- Congratulations to Zhong Jun for becoming the maintainer of chaos metric model working group
- [receive] new benefits of 60 yuan / year? Lowest in history! Double 11 has now begun to seize resources! Get started quickly!!
- Exploration of web application component automatic discovery
- What is a 1U server? What industries can 1U servers be used in?
- Worthington脱氧核糖核酸酶I特异性和相关研究
- How does the compiler put the first instruction executed by the chip at the start address of the chip?
- Go language Chanel memory model
- Clickhouse thread occupation troubleshooting and killing
猜你喜欢

由浅入深的混合精度训练教程

Abnova荧光原位杂交(FISH)探针解决方案

Kubernetes 资源拓扑感知调度优化

Brief ideas and simple cases of JVM tuning - how to tune

多任务视频推荐方案,百度工程师实战经验分享

How does the compiler put the first instruction executed by the chip at the start address of the chip?

15+ urban road element segmentation application, this segmentation model is enough
![Web technology sharing | [map] to realize customized track playback](/img/b2/25677ca08d1fb83290dd825a242f06.png)
Web technology sharing | [map] to realize customized track playback

Changjiang Dayong, director of openeuler community: jointly promote the new open source model of Euler and jointly build a new open source system

The results of the 2022 open source summer were announced, and 449 college students will contribute to open source projects
随机推荐
How much space does structure variable occupy in C language
web渗透测试----5、暴力破解漏洞--(9)MS-SQL密码破解
Use the fluxbox desktop as your window manager
多任务视频推荐方案,百度工程师实战经验分享
Tencent ECS installs the Minio object storage tool
How to be a web server and what are the advantages of a web server
Several good books for learning data
What is etcd and its application scenarios
From virtual to real, digital technology makes rural funds "live"
What is FTP? How does the ECS open the FTP protocol?
Download files and close the enhanced module security configuration to visit the website for the first time using IE browser
3. go deep into tidb: perform optimization explanation
Worthington木瓜蛋白酶化学性质和特异性
How to spell the iframe address of the video channel in easycvr?
Changjiang Dayong, director of openeuler community: jointly promote the new open source model of Euler and jointly build a new open source system
TCP three handshakes and four waves
How to set the domain name on the server what is the role of the domain name
Troubleshoot the high memory consumption of Go program
mysql中表的命名
Real time monitoring of water conservancy by RTU of telemetry terminal