当前位置:网站首页>程序猿入门攻略(十一)——结构体
程序猿入门攻略(十一)——结构体
2022-06-30 06:28:00 【Yuan_o_】
1、结构体的声明
1.1 结构的基础知识
区分:
结构是一种集合。
数组十一组相同类型的集合。
C语言中的内置类型(char short int long float double…)只能表示单一的量,
而生活中经常有复杂对象需要我们去表示,如:
人:名字+电话+性别+身高
书:书名+作者+定价+书号
复杂对象的描述就会使用到:结构体
结构是一些值得集合,这些值成为成员变量。结构的每个成员可以是不同类型的变量。
1.2 结构的声明

例如描述一个人:
struct Peo
{
char name[20];//名字
char tele[12];//电话
char sex[5];//性别
int high;//身高
};


1.3 结构体成员的类型
结构体的成员可以是标量、数组、指针、甚至是其他结构体。
1.4 结构体变量的定义和初始化



补充:
打印方式及结果:
2、结构体成员的访问
#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);//结构体指针.成员变量
}
void print2(struct Peo* sp)
{
printf("%s %s %s %d\n", sp->name,sp->tele,sp->sex,sp->high);//结构体变量->成员变量
}
int main()
{
struct Peo p1 = {
"张三","18888888888","男","188" };//结构体变量的创建
struct St s = {
{
"李四","16666666666","女","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);//传结构体
print2(&p1);//传结构体地址
return 0;
}

3、结构体传参

- 在print1函数中,直接把结构体传参,形参是实参的一份临时拷贝,所以print1函数在内存中又开辟了一块和p1一样大的空间,并且要把p1中的内容传递过去,这样既浪费了空间,又浪费了时间。
- 在print2函数中,只是把结构体的地址传递过去,无非就是需要4/8个字节的空间来保存这个地址,大大节省了空间,时间。
总结:
函数传参的时候,函数是需要压栈的。
如果传递一个结构体对象的时候,结构体过大,函数压栈的系统开销比较大,所以会导致性能的下降。
所以,结构体传参的时候,要传结构体的地址。
边栏推荐
- Share problems solved
- A small template (an abstract class, a complete process is written in a method, the uncertain part is written in the abstract method, and then the subclass inherits the abstract class, and the subclas
- Vscode configuration proxy
- Go pack and unpack
- Uniapp wechat applet returns to the previous page and refreshes
- Static routing job
- RSA and AES
- Record a problem tracking of excessive load
- Loading class `com. mysql. jdbc. Driver‘. This is deprecated. The new driver class is `com. mysql. cj. jdb
- 880. decoded string at index
猜你喜欢

不忘初心,能偷懒就偷懒:C#操作Word文件

IP TCP UDP network encryption suite format

Common NPM install errors

Mariadb数据库的安装与初始化

Zibll sub theme v6.4.1wordpress open source download_ Crack the original / use it directly / no tutorial required

1.9 - 存储器的分类

Jgaurora A8 configuration file
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

1.4 - 定点数与浮点数

Base64 explanation: playing with pictures Base64 encoding
随机推荐
深度学习---三好学生各成绩所占权重问题(3)
INI analyse les documents d'apprentissage
Share problems solved
Rhcsa day 1
【我的创作纪念日】一周年随笔
Zibll子比主题V6.4.1wordpress 开心版源码下载_破解原版/直接使用/无需教程
Collection and method of traversing collection elements (1)
1.7 - CPU的性能指标
Connect to remote server
DOM (document object model) document XML file object model
HCIA day 1
HuaWei满级大牛首次分享出这份598页网络协议全彩手册
ES6 arrow function
Cocos studio3.1 installation package win
gazebo/set_ model_ State topic driving UAV model through posture
Use and principle of completionservice (source code analysis)
Inner member of class 5: inner class
Problems related to pinduoduo store search, why can't new products be found on the shelves
Use of observer mode and status mode in actual work
Common mistakes daily practice 01