当前位置:网站首页>[advanced C language] user defined type
[advanced C language] user defined type
2022-06-30 00:10:00 【Haozai lives today】
List of articles
1、 Structure
1.1、 Structure declaration

A special case :
If the name of the structure is ignored in the declaration , Anonymous structure ( It's best not to be anonymous to the structure )
1.2、 Structure self reference
The correct way of structure self reference
struct Node
{
int data;
struct Node* next;
};
int main()
{
return 0;
}
1.3、 Structure variable definition and initialization
Various types of structure initialization methods
#include <stdio.h>
typedef struct Node
{
int data;
struct Node* next;
}Node;
struct stu
{
char name[20];
char sex[5];
int age;
int height;
}s2,s3,s4;// Variable list (s2,s3,s4 Is the global structure variable )
struct Data// Nested structures within structures
{
struct stu s;
char ch;
double;
};
int main()
{
struct Node n2 = {
100,NULL };// Initialize with braces
struct stu s1 = {
"zhangsan","nan",20,180 };
struct Data d = {
{
"lisi","nv",30,166},'w',3.14 };
// How nested structures are initialized
return 0;
}
1.4、 Structure memory alignment ( a key )
1、 The first member of the structure , Stored at the beginning of the structure variable 0 Offset
2、 Start with the second member , Align to Align numbers At an integer multiple address of
3、VS Default in phase Align numbers by 8, The smaller value of the member's own size and the default alignment number , Is the alignment number
4、 The total size of the structure must be Maximum number of alignments Integer multiple
5、 Maximum number of alignments It refers to the largest alignment number of all members
6、 If the structure is nested , 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
7、 We can go through #pragma pack To change the default alignment number of structure memory
#pragma pack (1)// The default number of alignments is changed to 4, Change it to 1 That is, it is not aligned , The general revision is 2,4,6……
struct S
{
double d;
char c;
};
#pragma pack()
PS:Linux There is no default alignment number in the environment , The number of alignments is the size of the member itself
1.5、 Structural parameters
1、 There are two ways to transfer parameters from a structure , The first is value transfer , The other is the address
2、 Passing values will cause a lot of memory space waste , Therefore, generally choose the transmission address
Little knowledge
1、typedef yes C Keywords in language , Its effect is Define a new name for a data type
2、 Why is there structure memory alignment
Performance reasons : To access unaligned memory , The processor needs to do two memory accesses , The aligned memory access only needs one access , So structure memory alignment is Trade space for time How to do it , So when we design the structure , We have to satisfy the alignment , And save space , Need to let Members who take up less space try to get together
3、ofsetof() Is a macro , Returns the offset of the structure member in memory
#include<stdio.h>
#include<stddef.h>
struct S
{
char c1;
int a;
char c2;
};
int main()
{
//offsetof() return Structural members Offset in memory
printf("%d\n", offsetof(struct S, c1));//0
printf("%d\n", offsetof(struct S, a));//4
printf("%d\n", offsetof(struct S, c2));//8
return 0;
}
边栏推荐
- 西门子低代码 9.14版本: 满足不同需求
- This PMP Exam (June 25), some people are happy and others are worried. That's why
- Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query
- Embedded development: Hardware in the loop testing
- Machine learning: the concept and application of VC dimension
- Solr基础操作9
- Will the flush SQL CDC parallelism affect the order? Generally, only 1 can be set for data synchronization.
- label问题排查:打不开标注好的图像
- 这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...
- Summary of DOM knowledge points
猜你喜欢

QT learning 02 GUI program example analysis

Exploration and Practice on the future direction of byte cloud database

koa2学习和使用

Fine grained identification, classification, retrieval data set collation

Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached

Halcon practical: design idea of solder joint detection

Three postures of anti CSRF blasting

Summarize Flink runtime architecture in simple terms

Basic tutorial for installing monggodb in win10

Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
随机推荐
Web APIs environment object - dark horse programmer
Cacti maximum monitoring number test
Copy linked list with random pointer [space for time --hash record]
Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query
vsftp 与 TFTP 与 samba 与 nfs 复习
Koa2 learning and using
golang7_ TCP programming
Sword finger offer 13 Range of motion of robot
【微信小程序】认识小程序项目的基本组成结构
Sword finger offer 14- ii Cutting rope II
如何实现搜索引擎中的拼写纠错功能——思路
Solr基础操作6
Solr基础操作11
Will the flush SQL CDC parallelism affect the order? Generally, only 1 can be set for data synchronization.
QT learning 02 GUI program example analysis
Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached
Solr基础操作8
Leetcode (76) -- Minimum Covering substring
Andorid source build/envsetup.sh 该知道的细节
Analysis of define incdir command in TCL script of Modelsim