当前位置:网站首页>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. !
边栏推荐
- Rasa 3. X learning series -rasa version 3.1.5 release
- 读取二进制文件的中文乱码问题
- 为什么磁力变速齿轮会反转?
- 华为无线设备配置动态负载均衡
- 6G空天地一体化网络高空平台基站下行频谱效率研究
- Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
- 【附下载】带你使用frp实现内网穿透详细教程!
- 一个简单的websocket例子
- vim怎么保存后退出
- Simulation Research on optimal detection of fault data in communication network
猜你喜欢

潇洒郎:VMware固定虚拟机IP地址

VC view memory leak

SSH Connection Huawei modelarts Notebook

How to realize the association between interfaces in JMeter?

麒麟信安操作系统衍生产品解决方案 | 主机安全加固软件,实现一键快速加固!

如何解决谷歌浏览器解决跨域访问的问题
![[sort] merge sort](/img/74/80bd271203368f61190af3edd52bb8.png)
[sort] merge sort

Network Security Learning (Qianfeng network security notes) 1-- building virtual machines

【附下载】带你使用frp实现内网穿透详细教程!

Huawei wireless device configuration dynamic load balancing
随机推荐
文件操作的底层原理(inode与软硬链接,文件的时间属性)
半监督学习在恶意软件流量检测中的应用
koa2 连接 mysql 数据库实现增删改查操作
【森城市】GIS数据漫谈(四)— 坐标系统
Huawei Shengsi mindspire detailed tutorial
Microsoft OneNote 教程,如何在 OneNote 中插入数学公式?
2022年陕西省中职组“网络空间安全”—数据包分析
How to solve the problem of cross domain access by Google browser
Analysis of Web Remote Code Execution Vulnerability of Zhongke panyun-d module
一个简单的websocket例子
Flink introduction to actual combat - phase IV (time and window diagram)
laravel 生成分表脚本示例
Flink entry to practice - stage 5 (processing function)
opencv 画黑色矩形,并写上序号
HCIA 静态基础实验 7.8
[200 opencv routines] 233 Moment invariants of regional features
笔记本键盘失灵解决办法
【原创】Magisk+Shamiko过APP ROOT检测
如何在双链笔记软件中建立仪表盘和知识库?以嵌入式小组件库 NotionPet 为例
Random talk on GIS data (III)