当前位置:网站首页>Introduction to programming ape (11) -- structure
Introduction to programming ape (11) -- structure
2022-06-30 06:32:00 【Yuan_ o_】
1、 Declaration of a structure
1.1 Basic knowledge of structure
distinguish :
A structure is a collection .
Array eleven sets of sets of the same type .
C Built in types in languages (char short int long float double…) Can only represent a single quantity ,
In life, there are often complex objects that we need to express , Such as :
people : name + Telephone + Gender + height
book : Title + author + pricing + Book number
The description of complex objects will use : Structure
Structure is something worth collecting , These values become member variables . Each member of a structure can be a variable of a different type .
1.2 Statement of structure

For example, describe a person :
struct Peo
{
char name[20];// name
char tele[12];// Telephone
char sex[5];// Gender
int high;// height
};


1.3 Type of structure member
Members of a structure can be scalars 、 Array 、 The pointer 、 Even other structures .
1.4 Structure variable definition and initialization



Add :
Printing method and results :
2、 Access to structure members
#include <stdio.h>
struct Peo
{
char name[20];
char tele[12];
char sex[5];
int high;
};
struct St
{
struct Peo p;
int num;
float b;
};
void print1(struct Peo p)
{
printf("%s %s %s %d\n", p.name, p.tele, p.sex, p.high);// Structure pointer . Member variables
}
void print2(struct Peo* sp)
{
printf("%s %s %s %d\n", sp->name,sp->tele,sp->sex,sp->high);// Structural variable -> Member variables
}
int main()
{
struct Peo p1 = {
" Zhang San ","18888888888"," male ","188" };// Creation of structure variables
struct St s = {
{
" Li Si ","16666666666"," Woman ","166"},100,3.14f };
printf("%s %s %s %d\n", p1.name, p1.tele, p1.sex, p1.high);
printf("%s %s %s %d %d %f\n", s.p.name, s.p.tele, s.p.sex, s.p.high, s.num, s.b);
print1(p1);// Transmissive structure
print2(&p1);// Pass structure address
return 0;
}

3、 Structural parameters

- stay print1 Function , Directly transfer the structure to the parameter , A formal parameter is a temporary copy of an argument , therefore print1 Function opens up another block in memory and p1 It's the same size , And put p1 The contents of the pass through , This is a waste of space , Another waste of time .
- stay print2 Function , Just pass the address of the structure , Just need 4/8 Bytes of space to store this address , It saves a lot of space , Time .
summary :
When a function passes parameters , Functions need to be stacked .
If you pass a structure object , The structure is too large , The system overhead of function stack pressing is relatively large , So it can lead to performance degradation .
therefore , When structures transmit parameters , To transfer the address of the structure .
边栏推荐
- To: k210 realizes face recognition (code interpretation attached)
- IO streams (common streams)
- 图像处理7-图像增强
- Base64 explanation: playing with pictures Base64 encoding
- C语言:练习题三
- How to use unmarshaljson
- High performance distributed execution framework ray
- 46. full arrangement -dfs double hundred code
- Set in set (III)
- Picture.....
猜你喜欢

Usage of case, casez and casex statements in Verilog

多线程进阶篇
A complete performance test process

How does Altium designer hide some temporarily unnecessary classes, such as GND

Common NPM install errors

Gazebo model modification
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

C语言:练习题三

Px4 control mode summary

Beauty of Refactoring: when multithreaded batch processing task lifts the beam - Universal scaffold
随机推荐
Installing googleplay environment on Huawei mobile phones
Suggestion: use tools:overrideLibrary
Hao looking for a job
Gazebo installation, uninstall and upgrade
Summary of 2 billion redis data migration
Force buckle ------ replace blank space
1.9 - Cache
DXP copper laying settings
DHCP operation
不忘初心,能偷懒就偷懒:C#操作Word文件
[database] transaction
Variable parameters of go
华泰炒股安全吗?我想网上开户。
Beauty of Refactoring: when multithreaded batch processing task lifts the beam - Universal scaffold
Unable to access the Internet at win10 /11 hotspot
Potential bottleneck of redis
关注这场直播,了解能源行业双碳目标实现路径
ES6 arrow function
ini解析學習文檔
1.3 - 码制