当前位置:网站首页>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 .
边栏推荐
- Practice summary of Prometheus project in amu Laboratory
- Collection and method of traversing collection elements (1)
- Decompilation normal decompilation problems. Solve them yourself
- 图像处理7-图像增强
- Base64详解:玩转图片Base64编码
- MySQL summary
- 1.8 - 多级存储
- Redux source code implementation
- Multithreading advanced level
- DXP copper laying settings
猜你喜欢
A complete performance test process
File operation io-part1
Feisheng: Based on the Chinese word breaker ik-2 ways to build custom hot word separators Showcase & pit arrangement Showtime
How does Altium designer hide some temporarily unnecessary classes, such as GND
C language final experiment report (student achievement management system) source code
神经网络入门
Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
Jgaurora A8 configuration file
[wechat applet: single or multiple styles, background color, rounded corners]
文件操作IO-Part1
随机推荐
requests. The difference between session () sending requests and using requests to send requests directly
Beauty of Refactoring: when multithreaded batch processing task lifts the beam - Universal scaffold
880. 索引处的解码字符串
Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
我做功能测试这么多年的心得
Pycharm shortcut key
从底层结构开始学习FPGA----RAM IP核及关键参数介绍
Rhcsa day 3
When to use redis
与MQTT的初定情缘
Arrangement of in-depth learning materials
MySQL summary
一个完整的性能测试流程
Multithreading advanced level
Is it safe to open an account online? Can you open an account to speculate on the Internet?
Ten years' miscellaneous thoughts
uniapp 微信小程序返回上一页并刷新
Image processing 7- image enhancement
【我的创作纪念日】一周年随笔
File operation io-part1