当前位置:网站首页>On the structural types of C language
On the structural types of C language
2022-07-19 10:21:00 【kunmu_】
️C The structural types of language
Only creation is happiness , Only creatures created are creatures ----- Roman · Roland
List of articles
Why is there a construction type
stay C In the language we have learned int、float、char Basic data type , These data types can only It means that there are only some specific data , It can't completely represent everything in reality . For example, you want to express a person's characteristics , Then the name of this person , Age , Gender and other information cannot be completed with basic data types alone .
therefore C Language provides constructive types for us to create , Thus, it can express the characteristics of all things in the world .
Next , We will go into detail about Structure (struct)、 enumeration (enum)、 Consortium (union)
Structure
1、 What is a structure
A structure is made up of a group called members ( Member variables ) Composed of different data , Each of these members can have a different type . Structures are usually used to represent several data of different types but related --------- Baidu Encyclopedia
The members of the structure can be Array 、 Pointer to the variable 、 Even other structures , Structures are generally used to describe complex objects .
2、 Writing and format of structure
The structure adopts keyword struct To build
struct Peo //struct Keyword for structure , Used to create structures Peo Is the structure name
{
char name[20]; // In curly brackets is the list of structure members , Also called member variable
int age;
char id[20];
}xiaoming; //xiaoming To use a structure Peo Structure variables created , But this is a global variable , Not recommended . Note that the semicolon here cannot be lost !!!
3、 Structure variable definition and initialization
The definition of structure variables can be directly created by using the constructed structure .
The initialization of structure variables adopts { }, Between member variables commas Can then .
The specific code is as follows :
struct Stu
{
char name[20];
int age;
char id[20];
}p1, p2; //p1,p2 To express with struct Stu Two global variables created , Try to avoid global variables when writing code
struct Stu p3;
struct Stu p4; //p3,p4 For use struct Stu Two global variables created
int main()
{
struct Stu s1; // Use struct Stu Create local variables
struct Stu s2 = {
"zhangsan", 20, "1213141"}; // Use struct Stu Create local variables and initialize
return 0;
}
4、 Access to structure members
①: use == Dot operator (.)== To access structure members
(.) Receive two operands
struct Stu
{
char name[20];
int age;
char id[20];
};
int main()
{
struct Stu s2 = {
"zhangsan", 20, "1213141"};
printf("%s %d %s",s2.name, s2.age, s2.id); // Use (.) Operator to access structure members
return 0;
}
②: use ( -> ) The operator To access structure members
When what we get is not a structural variable , It is a pointer to a structural variable , And then we can use it ( -> ) Operator to access structure members
struct Stu
{
char name[20];
int age;
char id[20];
};
void print(struct Stu* s2) // Parameters are received with structure pointers
{
printf("%s %d %s\n",s2->name,s2->age,s2->id ); // Pointer of structure variable is ( -> ) Operator to access structure members
}
int main()
{
struct Stu s2 = {
"zhangsan", 20, "1213141"};
print(&s2); // Pass the address of the structure variable to print function
return 0;
}
5、 Structure memory alignment ( Calculate the size of the structure )
Since the structure type is also a data type , Of course, you can also pass sizeof To calculate the size of the structure
But unlike the basic data type , The size of the structure is not constant , It has a great relationship with the type of structure member variables .
Calculation rules :
①: The first member is associated with the structure variable The offset for the 0 The address of
②: Other member variables should be offset to Align numbers An integral multiple of the address of .
Align numbers = The compiler defaults to an alignment number and the smaller value of the member size (vs The default number of alignments is 8, Other compilers generally do not have a default alignment number )
③: The total size of the structure is Maximum number of alignments ( Each member has an alignment number ) Integer multiple .
④: 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
give an example :
struct S1
{
char c1;
int i;
char c2;
};
6、 matters needing attention
①: Because the structure has memory alignment problems , So when designing the structure , Try to gather the types with small space
②: The default alignment number can be through #pragma pack(number) Make changes .
③: When the structure passes parameters , It should be transmitted to the structure Address , It's not worth it , That is, address transmission without value transmission .
enumeration
1、 Enumeration writing and format
Enumeration adopts keyword enum To build
enum Day // enum For enumerating keywords , Used to create enumeration types ,Day Enumeration name
{
mon, // Those in curly brackets are possible values , The alignment is listed one by one .
Tues, // Separate them with commas
Wed,
Thur,
Fri,
Sat,
Sun
};
In enumeration , All possible value types in curly braces have values , The default first one is 0, Increase in order , You can also align for initial assignment .
union ( Shared body )
1、 What is union
Federation is also a special custom type , Variables defined by this type also contain a series of members , The characteristic is that these members Share a memory space ( So union is also called community )
2、 Joint writing and format
Joint adoption union Keyword to create
union Un // The basic writing format is the same as the structure
{
char c;
int i;
};
3、 Calculation and characteristics of joint size
The size of the union is different from the basic data type , Even different from the calculation rules of the structure .
Calculation rules of joint size :
①: 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
give an example :
union Un1
{
char c[5];
int i;
};
union Un2
{
short c[7];
int i;
};
int main()
{
printf("%d\n", sizeof(union Un1)); // The size is 8
printf("%d\n", sizeof(union Un2)); // The size is 16
return 0;
}
Characteristics of Union :
①: When the value of a member variable in the union changes , The values of other member variables will also change .
②: The size of the union is at least the size of the largest member variable .
summary
Custom types give us the ability to represent a variety of things , So that we have the ability to innovate . Each type has its value and significance , We should find and excavate the proper use of these types , Let's take our code capability to the next level !
come on. !
边栏推荐
猜你喜欢
Blender digital twin production tutorial
Introduction to blender automated modeling
【微信小程序】使出千手浮图—回滚式
如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例
快速判断站点是否存活的 3 种编程实现
ROV and AUV of underwater vehicle
中科磐云——网络空间安全抓包题目 B.pcap
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
opencv 画黑色矩形,并写上序号
【原创】Magisk+Shamiko过APP ROOT检测
随机推荐
Random talk on GIS data (III)
Network Security Learning (Qianfeng network security notes) 1-- building virtual machines
2022.07.14 暑假集训 个人排位赛(九)
上學=掙錢?無需繳納學費的神仙院校!
Ffmpeg record video, stop (vb.net, step on the pit, class library - 10)
R语言使用lm函数构建线性回归模型、使用subset函数指定对于数据集的子集构建回归模型(使用subset函数筛选满足条件的数据子集构建回归模型)
2022年湖南省中职组“网络空间安全”数据包分析infiltration.pacpng解析(超详细)
空天地海协同应用综述
Run yolov3 on Huawei modelarts_ coco_ detection_ dynamic_ AIPP sample
ash: /etc/apt/sources. List: insufficient permissions
Deadlock, thread and process explanation
Blender自动化建模入门
ash: /etc/apt/sources.list: 权限不够
【Unity技术积累】实现鼠标画线功能 & LineRenderer
Huawei wireless device configuration dynamic load balancing
SSH connection to Huawei modelarts notebook
Distinction between private key and public key -- Explanation of private key and public key
Cmake -- Notes
麒麟信安操作系统衍生产品解决方案 | 主机安全加固软件,实现一键快速加固!
如何解决谷歌浏览器解决跨域访问的问题