当前位置:网站首页>[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure
[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure
2022-06-10 19:35:00 【You and I are all mortals】
author : You and I are all mortals
Blog home page : You and I are all mortal blogs
Aphorisms : Time will not stay for anyone , And things and people , It's changing all the time . Every one of us , Also keep moving forward !
If you think the blogger's article is good , I hope you'll make it three times in a row ( Focus on , give the thumbs-up , Comment on ), Give me more support !!
series :
Niuke exercises go straight to

List of articles
Catalog
Preface
This article explains about C Declaration of structure type in language , Structure initialization , Structure member access , A series of knowledge points that are not easy for novices to master, such as structure transfer parameters
Tips : The following is the main body of this article , The following cases can be used for reference
Declaration of struct type
A structure is a combination of values , These values are called member variables , Each member of a structure can be a variable of a different type
Our previous array is a collection of elements of the same type , Structure we have seen char,short,int long,long long And so on are called built-in types , Describe a single type of , There are many complex objects in our life , For example, describing a person needs a name , Telephone , Gender , Height, etc , Describing a book requires a title , author , The unit price , wait , The structure is used to describe complex objects
// Declaration of a structure
struct peo1
{
char name[20];
char sex[5];
int high;
char tel[12];
};// In general, this
struct peo2// Structure name
{
char name[20];// Structure member list
char sex[5];
int high;
char tel[12];
}p1,p2;// Two global structure variables
int main()
{
struct peo1 p1;// Creation of structure variables
return 0;
}Created struct peo1 It's the type , It doesn't actually take up space , And created with this type p1 Variables take up space , It is equivalent to that the type is drawing , and p1 It's the house , The drawing does not occupy an area , Building a house will take up space
The members of a structure can be scalars , Array , The pointer , You can even include a structure in the structure , Note that the contained structure must exist , Otherwise it won't work
Structure initialization
Initializing a structure means that a value is given to the structure when it is created
struct peo
{
char name[20];
char sex[5];
int high;
char tel[12];
};
struct st
{
struct peo p;
int num;
float f;
};
int main()
{
struct peo p1 = { "zhangsan"," male ",184,"15545675449" };// Initialization of structure
struct st p2 = { {"lisi"," Woman ",167,"15545567544"},122,2.33 };
printf("%s %s %d %s\n", p1.name, p1.sex, p1.high, p1.tel);
printf("%s %s %d %s %d %f\n",p2.p.name,p2.p.sex,p2.p.high,p2.p.tel,p2.num,p2.f);
return 0;
}

Structure member access
Members of structure variables are accessed through point operators , Accept two operands
Structure pointers access members that point to variables , Sometimes what we need is not a structural variable , It's a pointer to a structure , Such as the function in the following code , An arrow is required to point to the access member
struct peo
{
char name[20];
char sex[5];
int high;
char tel[12];
};
void print1(struct peo* p1)
{
printf("%s %s %d %s\n", p1->name, p1->sex, p1->high, p1->tel);// Structure pointer . Member name
}
void print2(struct peo p1)
{
printf("%s %s %d %s\n", p1.name, p1.sex, p1.high, p1.tel);// Structural variable . Member name
}
int main()
{
struct peo p1 = { "zhangsan"," male ",184,"15545675449" };// Initialization of structure
print1(&p1);
print2(p1);
return 0;
}Structural parameters
void print1(struct peo* p1)
{
printf("%s %s %d %s\n", p1->name, p1->sex, p1->high, p1->tel);// Structure pointer . Member name
}
void print2(struct peo p1)
{
printf("%s %s %d %s\n", p1.name, p1.sex, p1.high, p1.tel);// Structural variable . Member name
}
int main()
{
struct peo p1 = { "zhangsan"," male ",184,"15545675449" };// Initialization of structure
print1(&p1);
print2(p1);
return 0;
}As a matter of fact, Chuan Shen has already been involved in the above , There are two types of function arguments , The first is to pass the address to , The second is to pass the structure , So which of these two ways is better ?
We learned before , When a function passes parameters , It needs to be pressed , If you pass a structure object , The passed parameter is a temporary copy of the argument , Opening up a space , Cause repetition and waste of space , When passing a past structure object , If the structure is too large , The system overhead of parameter stack pressing is relatively large , It can lead to performance degradation , And if we send the address , The address is 4 A or 8 Bytes , And receiving with a pointer is also 4 perhaps 8 Bytes to receive the address of the first element , We can also find the content of the argument
So when the structure passes parameters , Try to pass the address of the structure
Entrance to exercises
After reading these specific operations, it is not allowed , You can click on the top to practice some exercises , You can also have a casual look C Some language exercises , Practice multiple-choice questions and programming questions , Let your knowledge be consolidated , Click directly into the title to go directly to , In addition, if you want to get the qualification of internal promotion of large factories, you can also go and have a look :
Digression
Here the blogger inserts a digression , You can skip , I have a friend who has ruined his face for three years because of acne marks , It is recovering well now , If you have the same puzzles and problems , You can go to his circle of friends and ask him about it :lpy16128227
The effect is really good , I saw with my own eyes that he had no acne , Ask him for details
边栏推荐
- 金融行业的密钥及加密机制
- 2022.05.27(LC_647_回文子串)
- MySQL advanced Chapter 1 (installing MySQL under Linux) [i]
- Openssl1.1.1 VS2013-编译教程
- WordPress 6.0 “Arturo阿图罗” 发布
- Design and reality of JSP project laboratory management system based on SSM doc
- mysql8.0(新特性小结)
- APICloud可视化开发新手图文教程
- Explain the interview questions by holding chestnuts (interview, review and study)
- 2022.05.23(LC_300_最长递增子序列)
猜你喜欢

Design and implementation of online ordering system based on SSM Rar (project source code)

2022.05.28(LC_5_最长回文子串)

云图说|每个成功的业务系统都离不开APIG的保驾护航

C (pointer-02)
![[vulnhub range] janchow: 1.0.1](/img/b5/e3f0d213ee87cd60802ee3db79d10f.png)
[vulnhub range] janchow: 1.0.1

SAR图像聚焦质量评价插件

Sliding window maximum value problem

【代理】10分钟掌握正向代理和反向代理的本质区别

MATLAB 根据任意角度、取样点数(分辨率)、位置、大小画椭圆代码

Chapter II data type (I)
随机推荐
WordPress 6.0 "Arturo Arturo" release
mysql(17-课后练习题)
Apicloud visual development - one click generation of professional source code
掌握高性能计算前,我们先了解一下它的历史
Vs solution to garbled Chinese characters read from txt files (super simple)
超级简单的课程设计ssm学生管理系统(含源码简单添加、删除、修改、查询操作)
SQL statement to view the basic table structure and constraint fields, primary codes and foreign codes in the table (simple and effective)
mysql8.0(新特性小结)
我的第一部作品:TensorFlow2.x
SAR回波信号基本模型与性质
Ruixin micro rk1126 platform platform porting libevent cross compiling libevent
【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off
基于ssm在线订餐系统设计与实现.rar(项目源码)
VS从txt文件读取中文汉字产生乱码的解决办法(超简单)
一文帶你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
Live broadcast preview | a new era of social interaction, exploring new social experiences in the universe
MySQL数据库设计概念(多表查询&事务操作)
C知识练习
Developers changing the world - Yao Guang teenagers playing Tetris
Lingo12 software download and lingo language introduction resources