当前位置:网站首页>[C language] custom type summary
[C language] custom type summary
2022-07-18 12:22:00 【Life is made by oneself ~】
Custom type
One 、 Structure
1.1 Declaration of a structure
Use the example of address book to show :
struct PeoInfo
{
char name[10];// full name
int age;// Age
char sex[6];// Gender
char tel[13];// Telephone
char addr[20];// Address
}p1;//p1 Global variable
This is an ordinary statement , There is another way to declare : Declaration of anonymous structure .
struct
{
char name[10];// full name
int age;// Age
char sex[6];// Gender
char tel[13];// Telephone
char addr[20];// Address
}p1;//p1 Global variable
Anonymous structures can only create variables after declarations , It cannot be used in other places .
struct
{
int a;
char b;
float c;
}x;
struct
{
int a;
char b;
float c;
}a[20], * p;
int main()
{
p = &x;
return 0;
}
Note that this code is illegal , The compiler will regard the above two types as Different types .
1.2 Self reference of structure
struct Node
{
int data;
struct Node next;
};
Someone might write like this . But that's going to be a problem : Can't calculate Node Size . So use it correctly :
struct Node
{
int data;
struct Node* next;
};
1.3 Initialization of structure variables
struct Node
{
int data;
struct Node* next;
}n1;
struct Node n2;
Both methods of definition are ok .
Initialization should use {}, Containing nested is {} Riga {}.
struct A
{
int a;
int b;
};
struct B
{
int c;
struct A a;
};
int main()
{
struct A a = {
1, 2 };
struct B b = {
1, {
1, 1} };
return 0;
}
1.4 Memory alignment and size calculation of structure
There are detailed introductions and exercises :
Structure / Calculation of the size of the consortium
1.5 Why is there memory alignment ️️
1️⃣ Platform reasons
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 .
2️⃣ Performance reasons
data structure ( Especially stacks ) should Align as much as possible on natural boundaries .
The reason lies in , To access unaligned memory , The processor needs to do Two memory accesses ; and Aligned memory access requires only one access .

Suppose that 32 Bit machine ( Access four bytes at a time ) visit a:
On the whole :
Structure memory alignment is to take Space for time How to do it .
So to save space , We should design : Let the members who occupy less space gather together as much as possible .
Two 、 Bit segment
2.1 The concept of bit segment
struct A
{
int _a : 2;
int _b : 5;
int _c : 10;
int _d : 30;
};
It can be seen that there are two differences between him and the structure :
1) The member of the segment must be int、unsigned int or signed int ( It can also be char type ).
2) The member name of the segment is followed by a colon and a number .
2.2 Calculation of bit size
First of all, you need to know what the number after the colon means :
int Its size is 4 Bytes , The back 2 It only needs two bits . So the following numbers Can't go beyond his own type .
computing method :
it is to be noted that Unused space is wasted
struct A
{
int a : 1;
char b;
};
The size of this bit segment is also 8, Because you have to think about Memory alignment And different data types cannot use one space .
2.3 Bit segment memory allocation
struct S
{
char a:3;
char b:4;
char c:5;
char d:4;
};
struct S s = {
0};
s.a = 10;
s.b = 12;
s.c = 3;
s.d = 4;

2.4 The cross platform problem of bit segment
1) int It's uncertain whether a bit segment is treated as a signed number or an unsigned number .
2) 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 ).
3) 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 .
4) 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 .
3、 ... and 、 enumeration
3.1 Definition of enumeration type
enum Day// week
{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
Like week , Color, a finite thing, can be enumerated .enum Day Is enumeration type {} It is called enumeration constant .
3.2 Advantages of enumeration
1) Increase the readability and maintainability of the code
2) and #define The defined identifier comparison enumeration has type checking , More rigorous .
3) Prevent named pollution ( encapsulation )
4) Easy to debug
5) Easy to use , You can define more than one constant at a time
3.3 Use of enumeration
enum Day// week
{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
In fact, these enumeration constants have values , from 0 Start , Back up +1. You can also assign values by yourself :
enum Day// week
{
Mon = 1,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
This is from 1 Back up . The back can be straight Assign values with enumerated variables
Four 、 Consortium ( Shared body )
4.1 Definition of consortium
// Declaration of union type
union Un
{
char c;
int i;
};
4.2 Characteristics of the consortium
Because the consortium shares a space , So changing one variable will also change the other .
4.3 Calculation of the size of the consortium
There are detailed introductions and exercises :
Structure / Calculation of the size of the consortium
边栏推荐
猜你喜欢

Power Bi ---- what is a measure?

Openeuler knows: the solution of IP addr not finding IP

【Jailhouse 文章】Bao: A Lightweight Static Partitioning Hypervisor for Modern Multi-Core Embedded...

JVM tuning practice (detailed version)

openEuler 知:ip addr 查不到 ip 的解决方法

Image denoising using nlmeas

Introduction, installation et utilisation des outils en ligne de commande terraform
![Part 50 - Analysis of a query request header parameter [2022-07-14]](/img/f7/754b5abf6f818ecb3f9b410d457732.png)
Part 50 - Analysis of a query request header parameter [2022-07-14]

PostgreSQL每周人物采访

【微信小程序】简洁好用的icon(94/100)
随机推荐
谷歌 | 图神经网络预训练帮助了分子表征吗
ThreadLocal killed 11 people. I can't bear it
长安链介绍-02
Detailed explanation of assembly language programming skills (with examples)
Simulation of common functions in C language
Openeuler knowledge: common website
Terraform命令行工具介绍、安装、使用
Logic of automatic reasoning 02 propositional calculus
Excel-vba quick start (VII. Get cell objects)
C# 使用JObject解析嵌套json
Ftxui basic notes (Hello World)
长安链介绍-01
Power Bi ---- what is a measure?
模拟实现C语言中常用函数
PostgreSQL每周人物采访
Technology sharing | quick intercom -5g intercom
Power bi---- DAX explanation
Part 50 - Analysis of a query request header parameter [2022-07-14]
[idea] add VM options to idea
Leetcode 196 Delete duplicate email (2022.07.15)