c语言第10章结构体.ppt

上传人:max****ui 文档编号:6334243 上传时间:2020-02-23 格式:PPT 页数:41 大小:255KB
返回 下载 相关 举报
c语言第10章结构体.ppt_第1页
第1页 / 共41页
c语言第10章结构体.ppt_第2页
第2页 / 共41页
c语言第10章结构体.ppt_第3页
第3页 / 共41页
点击查看更多>>
资源描述
第10章结构体 C语言程序设计 c语言第8章结构体 2 内容 结构体类型定义结构体变量的定义结构体变量的引用结构体变量的初始化结构数组结构体和指针结构体与函数链表 C语言程序设计 c语言第8章结构体 3 一 结构体类型定义结构体是一种构造数据类型用途 把不同类型的数据组合成一个整体 自定义数据类型结构体类型定义 struct 结构体名 类型标识符成员名 类型标识符成员名 C语言程序设计 c语言第8章结构体 4 例structstudent intnum charname 20 charsex intage floatscore charaddr 30 结构体类型定义描述结构的组织形式 不分配内存 C语言程序设计 c语言第8章结构体 5 例structstudent intnum charname 20 charsex intage floatscore charaddr 30 structstudentstu1 stu2 二 结构体变量的定义先定义结构体类型 再定义结构体变量一般形式 间接定义 struct结构体名 类型标识符成员名 类型标识符成员名 struct结构体名变量名表列 C语言程序设计 c语言第8章结构体 6 说明结构体类型与结构体变量概念不同类型 不分配内存 变量 分配内存类型 不能赋值 存取 运算 变量 可以结构体可嵌套结构体成员名与程序中变量名可相同 不会混淆 C语言程序设计 c语言第8章结构体 7 三 结构体变量的引用引用规则结构体变量不能整体引用 只能引用变量成员 引用方式 结构体变量名 成员名 C语言程序设计 c语言第8章结构体 8 可以将一个结构体变量赋值给另一个同类型结构体变量结构体嵌套时逐级引用 C语言程序设计 c语言第8章结构体 9 四 结构体变量的初始化间接定义初始化 struct结构体名 类型标识符成员名 类型标识符成员名 struct结构体名结构体变量 初始数据 例structstudent intnum charname 20 charsex intage charaddr 30 structstudentstu1 112 WangLin M 19 200BeijingRoad C语言程序设计 c语言第8章结构体 10 例 建立一个学生的基本情况表 然后将其打印输出 include includestructstudent intnum charname 20 charsex intage floatscore voidmain structstudentstu1 stu2 stu1 num 10001 strcpy stu1 name zhang stu1 sex M stu1 age 19 stu1 score 88 stu2 stu1 printf stu1 d s c d 6 2f n stu1 num stu1 name stu1 sex stu1 age stu1 score printf stu2 d s c d 6 2f n stu2 num stu2 name stu2 sex stu2 age stu2 score stu2 num stu2 sex F strcpy stu2 name wang printf stu2 d s c d 6 2f n stu2 num stu2 name stu2 sex stu2 age stu2 score getch 思考 把stu1中的数据采用变量初始化的方式提供初始值 structstudentstu1 10001 zhang M 19 88 stu2 C语言程序设计 c语言第8章结构体 11 五 结构体数组结构体数组的定义常用形式 间接定义法structstudent intnum charname 20 charsex intage structstudentstu 2 C语言程序设计 c语言第8章结构体 12 结构体数组初始化 结构体数组引用 引用方式 结构体数组名 下标 成员名 C语言程序设计 c语言第8章结构体 13 例统计候选人选票 includestructperson charname 20 intcount leader 3 Li 0 Zhang 0 Wang 0 voidmain inti j charleader name 20 for i 1 i 10 i scanf s leader name for j 0 j 3 j if strcmp leader name leader j name 0 leader j count for i 0 i 3 i printf 5s d n leader i name leader i count 3 C语言程序设计 c语言第8章结构体 14 六 结构体和指针指向结构体变量的指针定义形式 struct结构体名 结构体指针名 例structstudent p 使用结构体指针变量引用成员形式 例intn int p n 10 structstudentstu1 structstudent p p num 101 p num 101 C语言程序设计 c语言第8章结构体 15 includestructstudent longintnum charname 20 charsex floatscore Structstudentstu 1 p voidmain p 例指向结构体的指针变量 C语言程序设计 c语言第8章结构体 16 指向结构体数组的指针 例指向结构体数组的指针 includestructstudent intnum charname 20 charsex intage voidmain structstudentstu 3 10101 LiLin M 18 10102 ZhangFun M 19 10104 WangMin F 20 structstudent p C语言程序设计 c语言第8章结构体 17 指向结构体自身的成员指针 includestructstudent intnum charname 20 charsex intage structstudent next stuctstudentstu1 stu2 voidmain structstudent p p C语言程序设计 c语言第8章结构体 18 七 结构体与函数1 结构体指针做参数2 结构体变量作参数3 结构数组作参数 C语言程序设计 c语言第8章结构体 19 例编写程序实现把已有的几个学生数据显示在屏幕上 要求用一个函数实现对某个学生数据的输出 includestructstudent intnum charname 20 charsex intage voidprintOne structstudent pStu voidmain inti structstudentstu 3 10101 LiLin M 18 10102 ZhangFun M 19 10104 WangMin F 20 for i 0 i 3 i printOne voidprintOne structstudent pStu printf d s c d n pStu num pStu name pStu sex pStu age 思考题 采用结构体变量作参数怎么修改 比较两者的区别 C语言程序设计 c语言第8章结构体 20 例编写程序实现从键盘接受10学生的数据并显示在屏幕上 要求用一个函数实现对所有学生数据的接收 再用一个函数求所有学生的成绩的平均值 includestructstudent intnum charname 20 charsex intscore voidscanAll structstudentaryStu intn voidprintAve structstudentaryStu intn voidmain inti structstudentstu 10 scanAll stu 10 printAll stu 10 getch C语言程序设计 c语言第8章结构体 21 voidscanfAll structstudentaryStu intn inti for i 0 i n i scanf d s c d C语言程序设计 c语言第8章结构体 22 八链表 动态分配存储根据需要开辟或释放存储单元相关函数malloc函数calloc函数free函数说明应包含malloc h或stdlib h C语言程序设计 c语言第8章结构体 23 malloc函数 函数原型typedefunsignedsize t void malloc size tsize 参数size 分配存储空间的字节数返回值若成功 返回指向分配区域起始地址的指针若失败 返回NULL C语言程序设计 c语言第8章结构体 24 free函数 函数原型voidfree void ptr 参数ptr 要释放的内存区地址说明释放prt指向的内存区释放后的内存区能够分配给其他变量使用 C语言程序设计 c语言第8章结构体 25 结构体的应用 链表 LinkList structnode intdata structnode next structnode head C语言程序设计 c语言第8章结构体 26 链表的操作 链表的建立从链尾到链头 新结点插入到链头从链头到链尾 新结点插入到链尾链表的遍历删除结点根据一定的条件 删除一个或多个结点插入结点根据一定的条件 把新结点插入到指定位置 C语言程序设计 c语言第8章结构体 27 建立链表 从链尾到链头 head head p p malloc sizeof structnode p data a i for i 0 i n i p next head C语言程序设计 c语言第8章结构体 28 建立链表 从链头到链尾 p next q q malloc sizeof structnode q data a i for i 0 i n i q next NULL p q C语言程序设计 c语言第8章结构体 29 遍历链表 p p next while p printf d p data C语言程序设计 c语言第8章结构体 30 删除结点 p next q next free q q p next if p next满足删除条件 C语言程序设计 c语言第8章结构体 31 插入结点 p next q q malloc sizeof structnode q data x if p满足插入条件 q next p next C语言程序设计 c语言第8章结构体 32 建立链表 从链头到链尾 p next q q malloc sizeof structnode q data a i for i 0 i n i q next NULL p q C语言程序设计 c语言第8章结构体 33 遍历链表 p p next while p printf d p data C语言程序设计 c语言第8章结构体 34 删除结点 p next q next free q q p next if p next满足删除条件 C语言程序设计 c语言第8章结构体 35 插入结点 p next q q malloc sizeof structnode q data x p if p满足插入条件 q next p next C语言程序设计 c语言第8章结构体 36 链表操作中需要注意的几个问题 注意考虑几个特殊情况下的操作链表为空表 head NULL 链表只有一个结点对链表的第一个结点进行操作对链表的最后一个结点进行操作最后一个结点的next指针应为NULL可以定义一个结构体类型用于表示结点的数据部分 以便于对数据的操作 C语言程序设计 c语言第8章结构体 37 内容 结构体类型定义结构体变量的定义结构体变量的引用结构体变量的初始化结构数组结构体和指针结构体与函数链表 C语言程序设计 c语言第8章结构体 38 例 建立10名学生的信息表 每个学生的数据包括学号 姓名和三门课的成绩 求出每个学生的总分和平均分 用子函数实现 并统计出每门课不及格 一般 60 85 和优秀 85 100 的学生人数 用子函数实现 C语言程序设计 c语言第8章结构体 39 C语言程序设计 c语言第8章结构体 40 课后作业 p295五 2 3 4 课后作业 P295 五 2 3 4
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!