C语言程序设计-结构体与共用体

上传人:恋****泡 文档编号:127488156 上传时间:2020-04-02 格式:PPT 页数:71 大小:2.56MB
返回 下载 相关 举报
C语言程序设计-结构体与共用体_第1页
第1页 / 共71页
C语言程序设计-结构体与共用体_第2页
第2页 / 共71页
C语言程序设计-结构体与共用体_第3页
第3页 / 共71页
C语言程序设计-结构体与共用体_第4页
第4页 / 共71页
C语言程序设计-结构体与共用体_第5页
第5页 / 共71页
点击查看更多>>
资源描述

《C语言程序设计-结构体与共用体》由会员分享,可在线阅读,更多相关《C语言程序设计-结构体与共用体(71页珍藏版)》请在金锄头文库上搜索。

1、第12章结构体与共用体 程序设计语言 C 2 本章主要内容 结构体数据类型 共用体数据类型 枚举数据类型 定义数据类型的别名 结构体变量 结构体数组 结构体指针的定义和初始化 结构体成员的引用 成员选择运算符 指向运算符 向函数传递结构体变量 结构体数组 结构体指针 动态数据结构 动态链表 3 只能定义单一的数据类型 反映事物单一属性 如定义学生成绩 floatscore 能定义复杂的数据类型 反映事物多个属性 存放相同数据类型的一组数据 如 floatscore 30 12 1从基本数据类型到抽象数据类型 4 4 12 1从基本数据类型到抽象数据类型 用户自己构造数据类型 复合数据类型由基本

2、数据类型迭代派生而来 表示复杂的数据对象典型的代表就是 结构体 抽象数据类型 AbstractDataType ADT 在复合数据类型基础上增加了对数据的操作抽象数据类型进而进化为 类 Class 这是一个跨时代的进步Class是Object Oriented的一个重要概念 5 12 2 1问题的提出12 2 2结构体类型 结构体模板 定义12 2 3结构体变量的定义12 2 4结构体变量的初始化12 2 5结构体变量的引用12 2 6结构体数组12 2 7结构体指针12 2 8结构体作为函数参数 12 2结构体 6 12 2 1问题的提出 一个学生的信息有学号 姓名 性别 年龄 成绩等一本图

3、书的信息有分类编号 书名 作者 出版社 出版日期 价格 库存量等如何描述和管理这些类型不同的相关数据 使用二维数组行吗 问题 7 12 2 1问题的提出 解决方案 1 独立的变量表示 8 12 2 1问题的提出 2 使用一维数组 age no sex name 分配内存不集中 寻址效率不高 对数组赋初值时 易发生错位 结构显得零散 不易管理 score 9 12 2 1问题的提出 解决方案 C语言引入了称为结构体的数据存储方式 结构体 是一种构造数据类型 它是由若干数据项组合而成的复杂数据对象 这些数据项称为结构体的成员 把关系紧密且逻辑相关的多种不同类型的变量 组织到统一的名字之下 占用相邻

4、的一段内存单元 结构体变量表示 把不同类型的数据组合成一个整体 10 12 2 2结构体类型定义 struct 结构体名 数据类型名1成员名1 数据类型名2成员名2 数据类型名n成员名n struct是关键字 不能省略 合法标识符可省 无名结构体 成员类型可以是基本型或构造型 以分号 结尾 structStudent charno 9 学号charname 20 姓名charsex 性别unsignedintage 年龄floatscore 成绩 structDate shortyear 年shortmonth 月shortday 日 11 12 2 2结构体类型定义 structStuden

5、t charno 9 学号charname 20 姓名charsex 性别shortintage 年龄floatscore 成绩 结构体类型定义描述结构的组织形式 注意 结构体类型只是用户自定义的一种数据类型 用来定义描述结构的组织形式 不分配内存 只有用它来定义某个变量时 才会为该变量分配结构类型所需要大小的内存单元 12 在结构体中数据类型相同的成员 既可逐个 逐行分别定义 也可合并成一行定义 就象一次定义多个变量一样 structStudent Info charno 9 学号charname 20 姓名charsex 性别unsignedintage 年龄unsignedintclas

6、sno 班级floatgrade 成绩 structStudent Info charno 9 name 20 sex unsignedintage classno floatgrade 12 2 2结构体类型定义 13 12 2 3结构体变量定义 struct结构体类型名 数据类型名1成员名1 数据类型名n成员名n struct结构体类型名变量列表 先定义结构类型 再定义结构变量 structStudent charno 9 charname 20 charsex unsignedintage floatscore structStudentstu1 stu2 定义一个类型为structst

7、udent结构体的变量 将会为该变量分配内存 大小是等于其所有成员变量的大小之和 sizeof structStudent 14 12 2 3结构体变量定义 定义结构体类型的同时定义结构体变量 struct 结构体类型名 数据类型名1成员名1 数据类型名n成员名n 变量名列表 structStudent charno 9 charname 20 charsex unsignedintage floatscore stu1 stu2 struct charno 9 charname 20 charsex unsignedintage floatscore stu1 stu2 或 无名结构体定义

8、15 12 2 3结构体变量定义 成员可以是结构体 结构体可以嵌套 structdate intmonth intday intyear structstudent intno charname 20 structdatebirthday stu structstudent intno charname 20 structdate intmonth intday intyear birthday stu 16 12 2 4结构体变量的初始化 定义结构体变量时给结构体成员赋值 注意 赋初值时 中间的数据顺序必须与结构体成员的定义顺序一致 否则就会出现混乱 structStudentstu 091

9、22325 YangFan M 19 90 structStudentstu 19 YangFan M 09122325 90 17 12 2 4结构体变量的初始化 定义结构体变量时给结构体成员赋值 structdate intyear month day structStu Info charno 9 学号charname 20 姓名charsex 性别structdatebirthday 生日floatscore 成绩 structStu Infostu 20020306 ZhangMing M 1986 12 10 90 18 12 2 5结构体变量的引用 引用规则 不能整体引用 只能引

10、用变量的成员 引用方式 结构体变量名 成员名 成员 分量 运算符结合性 从左向右 structstudent charno 9 charname 20 charsex unsignedintage floatscore stu1 stu2 if stu1 stu2 stu1 score 85 5 stu1 age stu2 age strcpy stu1 no 09122414 19 12 2 5结构体变量的引用 可以将一个结构体变量赋值给另一个结构体变量结构体嵌套时逐级引用 structstudent intno charname 20 structdate intmonth intday

11、intyear birthday stu1 stu2 stu1 birthday month 12 结构体变量名 成员名 子成员名 最低级子成员名 20 12 2 5结构体变量的赋值 strcpy stu1 no stu no strcpy stu1 name stu name stu1 sex stu sex stu1 age stu age stu1 score stu score structStudentstu strcpy stu no 09122424 strcpy stu name XuTeng stu sex M stu age 21 stu score 90 structSt

12、udentstu1 stu1 stu 如果在定义结构体变量时并未对其赋初始值 那么在程序中要对它赋值的话 就只能一个一个地对其成员逐一赋值 或者用已赋值的同类型的结构体变量对它赋值 21 12 2 5结构体变量应用举例 例 计算某个学生5门课的平均成绩 最高分和最低分 includestructStudent floatscore 5 floatavescore maxscore minscore voidmain inti structStudentm printf inputthescoreoffivecourse n for i 0 i 5 i 输入5门课的成绩scanf f m ave

13、score 0 m maxscore m score 0 m minscore m score 0 for i 0 im maxscore m maxscore m score i if m score i m minscore m minscore m score i m avescore 5 printf avescore 4 1f maxscore 4 1f minscore 5 1f n m avescore m maxscore m minscore 运行结果 设5门课的成绩为 7580869068 avescore 79 8maxscore 90 0minscore 68 0 22

14、 12 2 6结构体数组 元素为结构体类型的数组称为结构体数组 在实际应用中 经常用结构体数组来表示具有相同数据结构体的一个群体 例如一个班的学员档案 一个公司的职工档案等 结构体数组的定义 形式一 structStudent charno 9 name 20 sex unsignedintage floatscore structStudentstu 10 形式二 structStudent charno 9 name 20 sex unsignedintage floatscore stu 10 形式三 struct charno 9 name 20 sex unsignedintage

15、floatscore stu 10 23 12 2 6结构体数组与二维表的对应关系 结构体数组就相当于一张二维表 表的框架对应的就是某种结构体类型 表中的每一列对应该结构体的成员 表中每一行信息对应该结构体数组元素各成员的具体值 表中的行数对应结构体数组的大小 结构体类型Student structStudent charno 9 charname 20 charsex unsignedintage floatscore stu 10 24 12 2 6结构体数组的初始化 基本格式 struct结构体类型数组 size 初值表1 初值表n 例 全部初始化时维数可省 25 12 2 6结构体数组

16、的引用 引用格式 结构体数组名 下标 成员名 structStudent charno 9 charname 20 charsex unsignedintage floatscore stu 10 strcpy stu 0 name WangFei stu 1 age printf s stu 0 name 26 12 2 6结构体数组举例 include includestructperson charname 20 候选人姓名intcount 得票数 leader 3 Li 0 Zhang 0 Wang 0 voidmain inti j charleader name 20 printf inputname n while 1 统计候选人得票数 gets leader name 输入候选人姓名if strcmp leader name 0 0 输入为 0 结束break for j 0 j 3 j 比较是否为合法候选人if strcmp leader name leader j name 0 合法leader j count 得票数加1 for i 0 i 3 i 显示候选人得票

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号