当前位置:网站首页>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
边栏推荐
- Distributed Computing Experiment 2 Thread Pool
- 字节跳动岗位薪酬体系曝光,看完我真的酸了...
- 线程和进程之间的区别
- TCP协议详解
- 8.2学习记录
- 在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作?
- redis---分布式锁存在的问题及解决方案(Redisson)
- IDEA引入类报错:“The file size (2.59 MB) exceeds the configured limit (2.56MB)
- 实现加载驱动、得到数据库对象、关闭资源的代码复用,将代码提取到相应的工具包里边。优化程序
- 为什么手动启动GBase 8c数据库中GTM节点,起不来。显示“Run cmd failed:scp: /tmp/gtm_gtm1.server: Permission denied”
猜你喜欢
一天学会JDBC06:PrepaerdStatemtnt
给Unity Behavior Designer(Unity行为树) 的Can See Object 画圆锥辅助图
图的基本概念
线程的状态
RT-Thread Studio学习(十二)W25Q128(SPI)的读写
How many assertion methods are commonly used in JMeter?
1161. Maximum Level Sum of a Binary Tree
data:image/jpg; base64 format data is converted to image
全国职业院校技能大赛网络安全竞赛之应急响应
玩转TypeScript对象、对象作为参数进行函数传递、接口和内置对象[无敌态]
随机推荐
LeetCode 97. 交错字符串
redis---分布式锁存在的问题及解决方案(Redisson)
[想要访问若依后台]若依框架报错401请求访问:error认证失败,无法访问系统资源
【我想要老婆】
Distributed Computing Experiment 1 Load Balancing
金仓数据库KingbaseES客户端编程接口指南-JDBC(6. JDBC 大对象数据处理)
unittest使用简述
使用requests post请求爬取申万一级行业指数行情
MYSQL JDBC图书管理系统
智能健身动作识别:PP-TinyPose打造AI虚拟健身教练!
Typora颜色公式代码大全
从零开始C语言精讲篇6:结构体
MySQL group_concat()详解
leetcode 22.8.1 二进制加法
经典递归回溯问题之——解数独(LeetCode 37)
线程的状态
给Unity Behavior Designer(Unity行为树) 的Can See Object 画圆锥辅助图
解决循环依赖import cycle not allowed的最佳解决办法
【虚幻引擎UE】UE5基于Gltf加载插件实现gltf格式骨骼动画在线/本地导入和切换
在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作?