当前位置:网站首页>Explain C language 12 in detail (C language series)
Explain C language 12 in detail (C language series)
2022-07-28 21:23:00 【Cream dimple* ٩ ( ˊωˋ*)و*】
Catalog
The grammatical structure of the structure :
The relationship between structure and object :
Select the reason for the address call :
Preface :
Let's discuss with you this time C The structure of language , This section is just a brief introduction to the structure , For more detailed explanation, please refer to the sharing with you in the later small and medium-sized editors .
The concept of structure :
In Xiaobian's view, a structure is a set of values , But the types of values can be different .
The grammatical structure of the structure :

How to call a structure :
When an object is created in the main function, the structure is called , As shown in the figure below .

The relationship between structure and object :
The structure type is just like the drawing in our life , When we create a structure object in the main function , We can regard it as our house , When we want to build a house, the internal structure of the house needs drawings to show us . Similarly, when we create a structural variable , We need to know the specific types of this object .( As shown in the figure below )

Access to structure members :
. Access and -> visit
See the following code for details :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct B
{
char c;
short s;
double d;
};
struct stu
{
struct B sb;
char name[20];
int age;
char id[20];
};
int main()
{
struct stu s = { {'w',20,3.14}," Zhang San ",30,"202220034" };
// use “.” visit
printf("%c\n", s.sb.c);
// use “->” visit
printf("%s\n", s.id);
// When using pointer access
struct stu* ps = &s;
// use “.” visit
printf("%c\n", (*ps).sb.c);
// use “->” visit
printf("%s\n", ps->id);
return 0;
}The code runs as follows :
Structural parameters :
Value transfer call :
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
// Value transfer call
#include<stdio.h>
struct B
{
char c;
short s;
double d;
};
struct stu
{
struct B sb;
char name[20];
int age;
char id[20];
};
void print1(struct stu t)
{
printf("%c %d %lf %s %d %s\n", t.sb.c, t.sb.s, t.sb.d, t.name, t.age, t.id);
}
int main()
{
struct stu s = { {'w',20,3.14}," Zhang San ",30,"202220034" };
print1(s);// Value transfer call
return 0;
}The code screenshot is as follows :

shortcoming : Most of the space is wasted when calling by value .
Address call :
The code is as follows :
#define _CRT_SECURE_NO_WARNINGS 1
// Address call
#include<stdio.h>
struct B
{
char c;
short s;
double d;
};
struct stu
{
struct B sb;
char name[20];
int age;
char id[20];
};
void print2(struct stu* ps)
{
printf("%c %d %lf %s %d %s\n", ps->sb.c, ps->sb.s, ps->sb.d, ps->name, ps->age, ps->id);
}
int main()
{
struct stu s = { {'w',20,3.14}," Zhang San ",30,"202220034" };
print2(&s);// Address call
return 0;
}The code screenshot is as follows :

advantage : Higher efficiency of parameter transmission , And you can change the original value .
Select the reason for the address call :
When a function passes parameters , Parameters need to be stacked . If you pass a structure object , The structure is too large , The system overhead of parameter stack pressing will be relatively large , So it will lead to performance degradation .
Conclusion :
When structures transmit parameters , Pass the address of the structure .
Conclusion :
This editor mainly shares a small part of knowledge about structure with you , Pay attention to small make up , Xiaobian will take you to continue to explore the deeper knowledge behind , Remember to like it, or you won't find it next time ! I hope that's helpful , If there are any mistakes in the article, you are also welcome to point out the maze for Xiaobian in time ( I'd like to thank you guys first !)
边栏推荐
- 4.1 various calling methods of member
- 关键路径的分析
- MySQL
- 证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结
- Quii Cordova plugin telerik imagepicker plug-in multi image upload out of sequence
- 1945. 字符串转化后的各位数字之和
- 上市1个月接连发生两起安全事故,理想L9还理想吗?
- [Zhou Zhou has a prize] cloud native programming challenge "edge container" track invites you to fight!
- 【题目】两数相加
- 4.2 Virtual Member Functions
猜你喜欢

实现瀑布流效果

怎么理解数据网格(Data Mesh)

MySQL sorts out the review content -- with mind map

Deit: attention can also be distilled

Several skills of API interface optimization

程序员最大的浪漫~

Ctfshow network lost track record (2)

Confession of a graduate student: why am I addicted to opengauss community?

Unity3d tutorial notes - unity initial 02

C language function program example (super complete)
随机推荐
微服务架构下的系统集成
ctfshow 网络迷踪做题记录(2)
Unity3d tutorial notes - unity initial 03
[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles
Maxwell is an easy-to-use software for capturing MySQL data in real time
探讨:想要落地DevOps的话,只考虑好的PaaS容器平台就够了么?
如何度量软件架构
Capture video by buffering
A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year
Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind
ctfshow 做题 web模块 web11~web14
BUUCTF做题Upload-Labs记录pass-01~pass-10
智能家居行业发展,密切关注边缘计算和小程序容器技术
Go concurrent programming basics
Cobal Strike的学习与使用
ctfshow 网络迷踪做题记录(1)
证券企业基于容器化 PaaS 平台的 DevOps 规划建设 29 个典型问题总结
The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......
MFC WPF WinForm (Industrial MFC or QT)
Confession of a graduate student: why am I addicted to opengauss community?