当前位置:网站首页>打卡: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
边栏推荐
- New ORM framework -- Introduction to beetlsql
- Experiment 5 components and event handling
- This new feature of C 11, I would like to call it the strongest!
- Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]
- LoadRunner - performance testing tool
- 场景题:A系统如何使用B系统的页面
- IDEA查看历史记录【文件历史和项目历史】
- Preview of converting doc and PDF to SWF file
- Explanation keyword of MySQL
- Do you really understand hashcode and equals???
猜你喜欢

移植tslib时ts_setup: No such file or directory、ts_open: No such file or director

ASP. Net 6 middleware series - conditional Middleware

2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination

After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path

2022 P cylinder filling training test questions and simulation test

MySQL之explain关键字详解

超好用的Excel异步导出功能

“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
![General testing technology [1] classification of testing](/img/f1/d80b6793b6443cbc4048d7e6319f51.png)
General testing technology [1] classification of testing

MySQL query specifies that a row is sorted to the first row
随机推荐
A set of combination boxing to create an idea eye protection scheme
移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
Quartz. Www. 18fu Used in net core
Why is bi so important to enterprises?
socket编程 send()与 recv()函数详解
Web Course Design - his system
2022年做跨境电商五大技巧小分享
Optimization of especially slow startup in idea debugging mode
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
集合之List接口
C introduction of variable parameter params
Detailed explanation of socket programming send() and recv() functions
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
Swap the left and right of each node in a binary tree
EasyUI's combobox implements three-level query
Cefsharp stores cookies and reads cookies
月薪10k-20k都无法回答的事务问题,你会吗?
队列的存储和循环队列
Yes Redis using distributed cache in NE6 webapi
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案