当前位置:网站首页>C Language Lectures from Scratch Part 6: Structure
C Language Lectures from Scratch Part 6: Structure
2022-08-04 08:03:00 【Gaoyou Wu Shao】
文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、结构体类型的声明
1.1结构的基础知识
结构是一些值的集合,这些值称为成员变量.结构的每个成员可以是不同类型的变量
We have now learnedchar、int、short、float等等类型,But these types are isolated.
If you want to describe a complex object,Say you describe a book,Books have a price、书名、作者、出版号…
To be able to describe complex objects,There is a structure.
And because complex objects have different properties,So there are different member variables,Different member variables may have different types
1.2结构的声明
struct tag
{
member-list;//成员列表
}variable-list;//变量列表,可以有,可以没有
例如描述一本书
struct Book
{
char name[20];
char author[15];
float price;
};//There is a semicolon here,别忘记了
1.3结构成员的类型
结构的成员可以是标量、数组、指针,甚至是其他结构体.
二、结构体变量的定义及初始化
2.1The definition of the variables of the structure
#include<stdio.h>
struct Book
{
char name[20];
char author[15];
float price;
}b1,b2;//Can be created directly behind the struct(一个或多个)变量
//这里的b1,b2是全局变量,在静态区
int main()
{
//struct Book 看做一个类型
struct Book b3;//也可以在mainCreate variables in the function
//这里的b3是局部变量,在栈区
return 0;
}
If you think it's too hard to write every time “struct 结构体名” to create variables hassle,可以使用typedefto rename the type
typedef struct Student
{
char name[20];
int age;
char id[20];
}Stu;//把类型struct Student重命名为Stu
//StuYou can no longer follow variables(After the rename, no variable list is allowed)
int main()
{
struct Student s1;
Stu s2;//s1和s2是一个类型
return 0;
}
2.2Initialization of variables of a structure
struct Student
{
char name[20];
int age;
char id[20];
};
int main()
{
struct Student s1 = {
"张三",20,"2016486001"};
struct Student s2 = {
"李四",19,"2016486002"};
return 0;
}
It is possible to encounter nested definitions,Then our initialization also needs to be nested
struct M
{
int a;
char b;
};
struct N
{
struct M m;
float c;
};
int main()
{
struct N t = {
{
1,'x'},3.14 };
return 0;
}
Declarations do not take up space,Definitions take up space.
Statements are like drawings for building a house,The definition is to build the house according to the drawings.
三、结构体成员的访问
结构体变量访问成员 结构变量的成员是通过点操作符(.)访问的.
点操作符接受两个操作数.例如:
struct M
{
int a;
char b;
};
struct N
{
struct M m;
float c;
};
int main()
{
struct N t = {
{
1,'x'},3.14 };
printf("%d\n", t.m.a);//The dot can be used again“.”to access the members of the inner structure
printf("%c\n", t.m.b);
printf("%f\n", t.c);
return 0;
}
但有时,What we get is not the structure itself,but the address of the structure.
这时,What if we want to access struct members?
struct M
{
int a;
char b;
};
struct N
{
struct M m;
float c;
};
int main()
{
struct N t = {
{
1,'x'},3.14 };
//printf("%d\n", t.m.a);//You can also use the dot again“.”to access the members of the inner structure
//printf("%c\n", t.m.b);
//printf("%f\n", t.c);
struct N* pt = &t;//获取结构体t的地址
//法一
printf("%d %c %f\n",(*pt).m.a, (*pt).m.b, (*pt).c);
//*pt,对pt进行解引用,就得到了t,(*pt).m就相当于t.m
//法二
printf("%d %c %f\n", (pt->m).a, (pt->m).b,pt->c);
//pt是指向结构体t的指针,m是结构体t的成员变量
//We prescribe a way of writingpt->m就相当于t.m
return 0;
}
四、结构体传参
请看下面代码:
struct S
{
int data[1000];
int num;
};
struct S s = {
{
1,2,3,4}, 1000 };
//结构体传参
void print1(struct S s)
{
printf("%d\n", s.num);
}
//结构体地址传参
void print2(struct S* ps)
{
printf("%d\n", ps->num);
}
int main()
{
print1(s); //传结构体
print2(&s); //传地址
return 0;
}
上面的 print1 和 print2 函数哪个好些?
答案是:首选print2函数. 原因:
函数传参的时候,参数是需要压栈的. 如果传递一个结构体对象的时候,结构体过大,参数压栈
的的系统开销比较大,所以会导致性能的下降.
But if the parameter you pass is just an address,也就是4个字节(64位下8字节)啊,This is much more performance friendly
边栏推荐
- leetcode 22.7.31(1)两数之和 (2)整数除法
- int *p = &a、p = &a、*p = a的正确理解
- GBase 8c中怎么查询数据库配置参数,例如datestyle。使用什么函数或者语法呢?
- 「PHP基础知识」转换数据类型
- 分布式计算实验3 基于PRC的书籍信息管理系统
- 25.时间序列预测实战
- RT-Thread Studio学习(十二)W25Q128(SPI)的读写
- Distributed Computing Experiment 2 Thread Pool
- The difference between character stream and byte stream
- LeetCode 135. 分发糖果
猜你喜欢
babylon 里面加gltf 模型
Yolov5更换主干网络之《旷视轻量化卷积神经网络ShuffleNetv2》
data:image/jpg;base64格式数据转化为图片
虚拟机没有USB网卡选项怎么解决
IDEA引入类报错:“The file size (2.59 MB) exceeds the configured limit (2.56MB)
秒懂大模型 | 3步搞定AI写摘要
inject() can only be used inside setup() or functional components.
轻量化Backbone VGNetG成就“不做选择,全都要”轻量化主干网络
How many assertion methods are commonly used in JMeter?
中职网络安全竞赛C模块MS17-010批量扫描
随机推荐
(三)DDD上下文映射图——老师,我俩可是纯洁的男女关系!
Typora_Markdown_图片标题(题注)
js异步变同步、同步变异步
ConstraintSet of animation of ContrstrainLayout
a标签下载图片,不要预览
【论文笔记】—低照度图像增强—Supervised—RetinexNet—2018-BMVC
The national vocational skills contest competition of network security emergency response
分布式计算实验4 随机信号分析系统
千万级别的表分页查询非常慢,怎么办?
The difference between character stream and byte stream
占位,稍后补上
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
为什么手动启动GBase 8c数据库中GTM节点,起不来。显示“Run cmd failed:scp: /tmp/gtm_gtm1.server: Permission denied”
力扣 剑指 Offer 04. 二维数组中的查找
JMeter 常用的几种断言方法,你会几种呢?
此时已莺飞草长,愿世间美好与你环环相扣
从零开始C语言精讲篇6:结构体
金仓数据库KingbaseES客户端编程接口指南-JDBC(7. JDBC事务处理)
【电脑录制屏】如何使用bandicam录游戏 设置图文教程
轻量化Backbone VGNetG成就“不做选择,全都要”轻量化主干网络