当前位置:网站首页>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 .
边栏推荐
- How to use unmarshaljson
- Wuenda coursera deep learning course
- Using C language pure for loop to implement ilovey
- Altium designer creates multiple class networks to realize different lineweights of multiple networks
- Four ways to create multithreads
- Idea run SQL file
- Centos8 install redis
- Learn fpga---ram IP core and key parameters from the bottom structure
- c# - C#用fo-dicom对CT图像的PixelData进行处理和转换
- 46. 全排列-dfs双百代码
猜你喜欢

我做功能测试这么多年的心得

Gazebo model modification

Unclear about glide loading picture

从底层结构开始学习FPGA----RAM IP核及关键参数介绍

Feisheng: Based on the Chinese word breaker ik-2 ways to build custom hot word separators Showcase & pit arrangement Showtime

Idea run SQL file

Initial love with mqtt

Mariadb数据库的安装与初始化

圖像處理7-圖像增强

C language final experiment report (student achievement management system) source code
随机推荐
深度学习---三好学生各成绩所占权重问题(3)
Basic use of markdown
Share problems solved
High performance distributed execution framework ray
Decompilation normal decompilation problems. Solve them yourself
Picture.....
List in set (2)
关于Glide加载图片模糊不清楚
圖像處理7-圖像增强
神经网络入门
Zibll sub theme v6.4.1wordpress open source download_ Crack the original / use it directly / no tutorial required
Collections tool class (V)
INI analyse les documents d'apprentissage
图片。。。。。
Completabilefuture: from understanding to mastering, here are all you want to know
c# - C#用fo-dicom对CT图像的PixelData进行处理和转换
01. 正则表达式概述
判断h5在两端是在微信环境还是企业微信环境
Wuenda coursera deep learning course
1.8 - 多级存储