当前位置:网站首页>[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
边栏推荐
- 【C语言】一不小心写出bug?凡人教你如何写出好代码【详解vs中调试技巧】
- 618大促将至,用AI挖掘差评,零代码实现亿级评论观点情感分析
- 腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
- openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC
- Analysis of optical storage direct flexible power distribution system
- nodejs-判断系统类型-获取主机名称-执行控制台命令-中文乱码
- 领域驱动设计(六) - 架构设计浅谈
- MySQL advanced Chapter 1 (installing MySQL under Linux) [i]
- Datascience & ml: detailed introduction to risk control indicators / field related concepts and dimension logic of risk control in the field of financial technology
- SAR回波信号基本模型与性质
猜你喜欢
随机推荐
叮咚抢菜-派送时段监听及推送工具
金融行业的密钥及加密机制
【C语言进阶】指针的进阶【中篇】
Sliding window maximum value problem
[Agency] 10 minutes to master the essential difference between forward agency and reverse agency
Nodejs basic architecture analysis parsing engine directory plug-in installation core module
618大促将至,用AI挖掘差评,零代码实现亿级评论观点情感分析
Adobe Premiere Basics - introduction, configuration, shortcut keys, creating projects, creating sequences (I)
mysql(17-触发器)
Debugging skills
基于ssm在线订餐系统设计与实现.rar(项目源码)
Apicloud visual development - one click generation of professional source code
[01] every high-quality author deserves to be seen. Let's take a look at this week's high-quality content!
openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC
Ruixin micro rk1126 platform platform porting libevent cross compiling libevent
WordPress 6.0 "Arturo Arturo" release
中国 璞富腾酒店及度假村旗下酒店推出全新水疗产品共庆6月11日全球健康日
Code solution of simplex method (including super detailed code notes and the whole flow chart)
【C语言进阶】指针的进阶【上篇】
Esp8266 system environment setup








