当前位置:网站首页>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 .
边栏推荐
猜你喜欢

My experience in functional testing for so many years
A complete performance test process

多线程进阶篇

Rhcsa day 1

Swoole process model diagram

图片。。。。。

1.4 - 定点数与浮点数

Gazebo model modification

Base64 explanation: playing with pictures Base64 encoding

Who doesn't want a blog site of their own - build a blog site WordPress
随机推荐
First experience of Galaxy Kirin
Rotate dimension tool rolabelimg
Simple example of class template
Spin official tutorial
Rhcsa day 3
【我的创作纪念日】一周年随笔
File Transfer Protocol,FTP文件共享服务器
Uniapp wechat applet returns to the previous page and refreshes
46. 全排列-dfs双百代码
VIM view file code
General contents of learning reinforcement learning
2020-10-06
Software tools_ Shortcut_ Operation summary
01. regular expression overview
DXP copper laying settings
Go common judgments
Installing googleplay environment on Huawei mobile phones
PHP knowledge points
High performance distributed execution framework ray
880. 索引处的解码字符串