当前位置:网站首页>打卡: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
边栏推荐
- Detailed explanation of socket programming send() and recv() functions
- 2022t elevator repair test simulation 100 questions and online simulation test
- 手机连接电脑后,QT的QDIR怎么读取手机文件路径
- Docker拉取mysql并连接
- IDEA查看历史记录【文件历史和项目历史】
- Improvement of ref and struct in C 11
- Fight leetcode again (290. Word law)
- MySQL keyword group_ Concat, combined connection query
- Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
- be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
猜你喜欢

2022t elevator repair test simulation 100 questions and online simulation test

. net webapi access authorization mechanism and process design (header token + redis)

ASP. Net 6 middleware series - conditional Middleware

MySQL keyword group_ Concat, combined connection query

How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing

xutils3修改了我提报的一个bug,开心

Top ten project management software similar to JIRA

Build websocket server in. Net5 webapi

关于idea调试模式下启动特别慢的优化

2022A特种设备相关管理(电梯)上岗证题库及模拟考试
随机推荐
通过 zxing 生成二维码
场景题:A系统如何使用B系统的页面
AWS from entry to actual combat: creating accounts
MySQL port is occupied when building xampp
Student achievement management
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
Super easy to use asynchronous export function of Excel
2022T电梯修理考试模拟100题及在线模拟考试
Find the number of leaf nodes of binary tree
JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
socket编程 send()与 recv()函数详解
This new feature of C 11, I would like to call it the strongest!
Mysql database, inconsistent index character set, slow SQL query, interface timeout
be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
一套组合拳,打造一款 IDEA 护眼方案
Establishing and traversing binary tree
Huawei mobile ADB devices connection device is empty
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
New ORM framework -- Introduction to beetlsql
Flink real-time data warehouse project - Design and implementation of DWS layer