当前位置:网站首页>打卡: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
边栏推荐
- 手机连接电脑后,QT的QDIR怎么读取手机文件路径
- 关于idea调试模式下启动特别慢的优化
- How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
- [mock data] fastmock dynamically returns the mock content according to the incoming parameters
- 二进制文件版本控制工具选择难?看完这篇你会找到答案
- MySQL installation pit
- Drawing polygons with < polygon / > circular array in SVG tag
- Experiment 5 components and event handling
- Using swagger in. Net5
- Advanced sorting - fast sorting
猜你喜欢
【无标题】
JS inheritance
软件测试相关知识~
Experiment 5 components and event handling
This new feature of C 11, I would like to call it the strongest!
PID debugging of coding motor (speed loop | position loop | follow)
2022年P气瓶充装培训试题及模拟考试
Top ten project management software similar to JIRA
超好用的Excel异步导出功能
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
随机推荐
可以接收多种数据类型参数——可变参数
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
Flink real-time data warehouse project - Design and implementation of DWS layer
General testing technology [1] classification of testing
The query type of MySQL is very inefficient.
Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
Student achievement management
Web Course Design - his system
二进制文件版本控制工具选择难?看完这篇你会找到答案
Xamarin effect Chapter 22 recording effect
2022T电梯修理考试模拟100题及在线模拟考试
socket编程 send()与 recv()函数详解
Flink实时数仓项目—DWS层设计与实现
幂等性实践操作,基于业务讲解幂等性
Fundamentals of software testing and development
批量下載文件----壓縮後再下載
通过 zxing 生成二维码
关于idea调试模式下启动特别慢的优化
First in the binary tree
Top 9 task management system in 2022