当前位置:网站首页>Introduction to C language custom types (structure, enumeration, union, bit segment)
Introduction to C language custom types (structure, enumeration, union, bit segment)
2022-06-24 04:23:00 【InfoQ】
1. Structure
1.1 Structure overview
1.1.1 Structure concept
1.1.2 Declaration and use of structures
// Structure
struct Stu
{
char name[20];// full name
char sex[5];// Gender
char id[20];// Student number
int age;// Age
double grade;// achievement
}student;
struct Stu Structural members student// Anonymous struct type
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// The wrong sample :
typedef struct NODE
{
int data;
Node* next;
}Node;
// The correct sample :
typedef struct NODE
{
int data;
struct NODE* next;
}Node;
#include <stdio.h>
int main()
{
struct Stu s = { " Zhang San "," male ","20210618",20,99 };// Structure declaration and initialization
printf(" full name :%s\n", s.name);// Structure member access method 1: Structure variable name . Structure member name
printf(" Gender :%s\n", s.sex);
struct Stu* ps = &s;
printf(" Student number :%s\n", ps->id);// Structure access member mode 2: Structure pointer -> Structure member name
printf(" Age :%d\n", ps->age);
printf("C Language test results :%.2lf\n", ps->grade);
return 0;
}
full name : Zhang San
Gender : male
Student number :20210618
Age :20
C Language test results :99.00
D:\gtee\C-learning-code-and-project\test_922\Debug\test_922.exe ( process 26272) Exited , The code is 0.
Press any key to close this window . . .
1.2 Structure alignment and its size calculation
1.2.1 Offset

1.2.2 Calculation of structure size
81#pragma pack(4)// Set the default alignment number to 4
#pragma pack()// No parameter means the default value is 8(vs compiler )
- Platform reasons ( Reasons for transplantation ): Not all hardware platforms can access any data on any address ; Some hardware platforms can only access certain types of data at certain addresses , Otherwise, a hardware exception will be thrown .
- Performance reasons :data structure ( Especially stacks ) It should be aligned as far as possible on the natural boundary . The reason is to access misaligned memory , The processor needs to make two memory accesses ; The aligned memory access only needs one access .
- [ ] The offset between the first member of the structure and the structure is
0.
- [ ] Structure members need to be aligned to an integer multiple of the alignment number ( Offset ) It's about .
- [ ] Each structure member has an alignment number , The alignment number is the smaller of the compiler default value and the structure member size .
- [ ] The total size of the structure is the maximum number of alignments ( The largest alignment number of each member of the structure ) Integer multiple .
- [ ] If there is a nested structure in the structure , The nested structure is aligned to an integral multiple of its maximum alignment , The overall size of the structure is the maximum number of alignments ( The number of alignments with nested structures ) Integer multiple .
// practice 1
struct S1
{
char c1;
int i;
char c2;
};
// practice 2
struct S2
{
char c1;
char c2;
int i;
};
// practice 3
struct S3
{
double d;
char c;
int i;
};
// practice 4- Structure nesting problem
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 ( process 34308) Exited , The code is 0.
Press any key to close this window . . .
1.3 Structure and bit segment
1.3.1 Bit segment
1.3.2 Bit segment implementation structure
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 ( process 22660) Exited , The code is 0.
Press any key to close this window . . .
816432_a230_b525_c1015_d30432_d88- The members of a segment can be
int unsigned intsigned intOr is itchar( It belongs to the plastic family ) type
- The space of the bit segment is based on 4 Bytes (
int) perhaps 1 Bytes (char) The way to open up .
- Bit segments involve many uncertainties , Bit segments are not cross platform ,Pay attention to portable program should avoid using bit segment .


intIt's uncertain whether a bit segment is treated as a signed number or an unsigned number .
- The number of the largest bits in the bit segment cannot be determined .(16 The machine is the largest 16,32 The machine is the largest 32, It's written in 27, stay 16 A bit of the machine will go wrong .
- The members in the bit segment are allocated from left to right in memory , Or right to left allocation criteria have not yet been defined .
- When a structure contains two bit segments , The second segment is relatively large , Cannot hold the remaining bits of the first bit segment , Whether to discard the remaining bits or to use , This is uncertain .

2. enumeration
2.1 Enumeration overview
2.1.1 Enumeration concept
2.1.2 Declaration and use of enumeration
enumenum Day// week
{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
enum Sex// Gender
{
MALE,
FEMALE,
SECRET
};
enum Color// Color
{
RED,
GREEN,
BLUE
};
Enumeration constants enum Color// Color
{
RED=1,
GREEN=2,
BLUE=4
};
enum Color// Color
{
RED=1,
GREEN=2,
BLUE=4
};
enum Color clr = GREEN;// Only enumeration constants can be assigned to enumeration variables , There will be no type difference .
clr = 5;// error , Enumeration constants cannot be assigned
2.2 Enumeration size calculation
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 ( process 21156) Exited , The code is 0.
Press any key to close this window . . .
2.3 The difference between enumeration and macro
#define#define - Increase the readability and maintainability of the code
- and
#defineThe defined identifier comparison enumeration has type checking , More rigorous .
- Prevent named pollution ( encapsulation ).
- Easy to debug .
- Easy to use , You can define more than one constant at a time .
3. Consortium
3.1 Overview of the consortium
3.1.1 Consortium concept
3.1.2 Declaration and use of the consortium
union// Declaration of union type
union Un
{
char c;
int i;
};
// The definition of joint variables
union Un un;
int is_bl()
{
union BL
{
char a;
int b;
}un;
un.b = 1;
// return 1 For the small end , return 0 Big end
if (un.a == 1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int ret = is_bl();
if (ret == 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}
The small end
D:\gtee\C-learning-code-and-project\test_922\Debug\test_922.exe ( process 23328) Exited , The code is 0.
Press any key to close this window . . .
3.2 Consortium size calculation
- [ ] 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 .
union Un1
{
char c[5];
int i;
};
union Un2
{
short c[7];
int i;
};
// What is the output below ?
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 ( process 12864) Exited , The code is 0.
Press any key to close this window . . .
边栏推荐
- openEuler Kernel 技术分享第 20 期 | 执行实体创建与切换
- Cloud development CMS Enterprise Edition demand survey
- The first 2021 Western cloud security summit is coming! See you in Xi'an on September 26!
- web渗透测试----5、暴力破解漏洞--(9)MS-SQL密码破解
- 抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
- Tsingsee Qingxi video easycvr integrated Dahua face recognition equipment
- [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
- I have an agreement with IOT
- web渗透测试----5、暴力破解漏洞--(8)PostgreSQL密码破解
猜你喜欢

Kubernetes resource topology aware scheduling optimization

共建欧拉社区 共享欧拉生态|携手麒麟软件 共创数智未来

mysql - sql执行过程

Flutter series: offstage in flutter

开源之夏2022中选结果公示,449名高校生将投入开源项目贡献

The results of the 2022 open source summer were announced, and 449 college students will contribute to open source projects
2020年Android面试题汇总(中级)

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

Weibo International Edition changed its name to Weibo light sharing Edition

What is etcd and its application scenarios
随机推荐
Jointly build Euler community and share Euler ecology | join hands with Kirin software to create a digital intelligence future
How to monitor multiple platforms simultaneously when easydss/easygbs platform runs real-time monitoring?
Web penetration test - 5. Brute force cracking vulnerability - (6) VNC password cracking
How to identify information more quickly and accurately through real-time streaming media video monitoring?
TCP three handshakes and four waves
How should the server be placed?
外网访问svn服务器(外网访问部署在云上的svn服务器)
Gpt/gpt2/dialogpt detailed explanation comparison and application - text generation and dialogue
How EDI changes supply chain management
What is etcd and its application scenarios
The results of the 2022 open source summer were announced, and 449 college students will contribute to open source projects
多任务视频推荐方案,百度工程师实战经验分享
After 20 years of development, is im still standing still?
What is FTP? How does the ECS open the FTP protocol?
Exploration of web application component automatic discovery
Can the video streams of devices connected to easygbs from the intranet and the public network go through their respective networks?
What is a 1U server? What industries can 1U servers be used in?
抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
How to draw the flow chart of C language structure, and how to draw the structure flow chart
Browser rendering mechanism