当前位置:网站首页>打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
2022-04-23 03:22:00 【每天都要学一点哦】
结构体
结构体是C语言中特别重要的知识点,结构体使得C语言有能力描述复杂类型。
比如描述学生,学生包含: 名字+年龄+性别+学号 这几项信息。
描述一本书,这本书包括:书名+出版社+目录+作者等信息
这些都是复杂的对象,C语言就给了自定义类型的能力
自定义类型中的有一种叫:结构体struct
结构体是把一些单一类型组合在一起的做法
例如,我们现在来描述一个人,他有名字+年龄+性别+学号 这几项信息
struct Stu
{
char name[20];//名字
int age; //年龄
char sex[5]; //性别
char id[15]; //学号
};
int main()
{
return 0;
}
我们要盖一个房子,struct Stu就是我们的图纸,创建一个结构体对象s,在里面根据图纸来填写数据
结构体的初始化
//打印结构体信息
struct Stu s = {
"张三", 20, "男", "20180101"};
可以把
struct Stu当成数据类型,s就是创建用来存放数据的结构体对象,s里面的数据就是成员名在struct Stu不使用的时候,里面的那些类型是不会开辟空间的
打印
//.为结构成员访问操作符
printf("name = %s age = %d sex = %s id = %s\n", s.name, s.age, s.sex, s.id);
要打印s里面的信息,要按照类型的顺序来打印,
"张三", 20, "男", "20180101"对应的%s,%d,%s,%s,顺序是不能颠倒的,且要按照结构体对象.成员名的形式
还有一个打印的方法
//
struct Stu *ps = &s;
printf("name = %s age = %d sex = %s id = %s\n", (*ps).name, (*ps).age, (*ps).sex, (*ps).id);
ps是结构体指针变量,这里就是指针那里的方法,不过有点麻烦,可以直接用->操作符
struct Stu *ps = &s;
printf("name = %s age = %d sex = %s id = %s\n", ps->name, ps->age, ps->sex, ps-
>id);
ps->name的意思就是ps指向s中的成员name,和(*ps).name的意思一样
版权声明
本文为[每天都要学一点哦]所创,转载请带上原文链接,感谢
https://blog.csdn.net/iqrmshrt/article/details/124357841
边栏推荐
- Query stored procedures in PostgreSQL
- 研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
- Course design of Database Principle -- material distribution management system
- Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
- MySQL之explain关键字详解
- Iotos IOT middle platform is connected to the access control system of isecure center
- Find the number of leaf nodes of binary tree
- 2022G2电站锅炉司炉考试题库及在线模拟考试
- Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
- The most understandable life cycle of dependency injection
猜你喜欢

OLED multi-level menu record
![Use of ADB command [1]](/img/e6/fb8fd6956c226f75b831f8eb46277f.png)
Use of ADB command [1]

IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统

MySQL keyword group_ Concat, combined connection query

Top ten project management software similar to JIRA

2022A特种设备相关管理(电梯)上岗证题库及模拟考试
![[vs Code] solve the problem that the jupyter file displays exceptions in vs code](/img/f6/a91d03fd140eb5f7688b72e2e6f2bb.png)
[vs Code] solve the problem that the jupyter file displays exceptions in vs code

“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
![[MySQL] left function | right function](/img/26/82e0f2280de011636c26931a74e749.png)
[MySQL] left function | right function

MySQL port is occupied when building xampp
随机推荐
JSON related
2022t elevator repair test simulation 100 questions and online simulation test
Knowledge of software testing~
Five tips for cross-border e-commerce in 2022
Mysql database design specification
Web Course Design - his system
Swap the left and right of each node in a binary tree
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
ASP. Net 6 middleware series - conditional Middleware
Mysql database
First in the binary tree
队列的存储和循环队列
全新的ORM框架——BeetlSQL介绍
Query stored procedures in PostgreSQL
超好用的Excel异步导出功能
Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
Flink实时数仓项目—DWS层设计与实现
12.<tag-链表和常考点综合>-lt.234-回文链表
[vs Code] solve the problem that the jupyter file displays exceptions in vs code