第11章_结构体与共用体_第1页
第11章_结构体与共用体_第2页
第11章_结构体与共用体_第3页
第11章_结构体与共用体_第4页
第11章_结构体与共用体_第5页
已阅读5页,还剩54页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第11章结构体与共用体 C程序设计 本章要点结构体的定义和引用结构体数组指向结构体类型数据的指针链表 主要内容概述 11 1 定义结构体类型变量的方法 11 2 结构体变量的引用 11 3 结构体变量的初始化 11 4 结构体数组 11 5 指向结构体类型数据的指针 11 6 用指针处理链表 11 7 共用体 11 8 用typedef定义类型 11 10 概述 11 1 C语言的数据类型 结构体struct 概述 11 1 为何引进结构体 一类问题学生的学号 姓名 性别 年龄等信息图书的书号 书名 出版社 价格等货物的货号 货名 进出货日期 价格等这类问题中数据有何特点 如何存储 特点由几个不同类型的数据组成一个有机整体 这个整体用一个数据元素表示 存储 需要按一定顺序同时存储这些不同类型的数据 结构体 一组具有不同数据类型的数据的有序集合 结构体的每个元素包含若干个不同数据类型的成员 结构体的各个元素结构相同 概述 11 1 结构体类型的声明一般形式 struct 结构体名 成员表列 类型名成员名 类型名成员名 如 structstudent intnum charname 20 charsex intage floatscore charaddr 30 不可或缺 说明 上述定义仅仅声明一个结构体类型 还未定义结构体变量 还未分配内存 定义结构体类型变量的方法 11 2 定义结构体变量的三种方式 1 先声明结构体类型再定义变量声明结构体类型struct结构体名 类型名成员名 类型名成员名 定义结构体变量struct结构体名变量名表列 如 structstudent intnum charname 20 charsex intage floatscore charaddr 30 structstudentstu1 stu2 定义结构体类型变量的方法 11 2 2 在声明类型的同时定义变量struct结构体名 类型名成员名 类型名成员名 变量名列表 structstudent intnum charname 20 charsex intage floatscore charaddr 30 stu1 stu2 struct intnum charname 20 charsex intage floatscore charaddr 30 stu1 stu2 3 直接定义结构体类型变量struct 类型名成员名 类型名成员名 变量名列表 定义结构体类型变量的方法 11 2 内存分配在定义了结构体变量后 系统为之分配的内存单元字节数是结构体变量中各个成员的字节数之和 例如 stu1和stu2在内存中各占63个字节 4 20 1 4 4 30 63 structstudent intnum charname 20 charsex intage floatscore charaddr 30 stu1 stu2 定义结构体类型变量的方法 11 2 关于结构体类型的说明 类型和变量是不同的概念类型 确定一类数据的结构与性质 变量 数据类型 不分配内存 变量 分配内存类型 不能赋值 存取 运算 变量 可以运算结构体变量的成员可以单独使用 相当于普通变量 成员名可以与程序中的变量名相同 互不干扰 结构体可以嵌套 成员也可以是一个结构体 定义嵌套的结构体类型时 应满足先定义后引用的原则 structstudent intnum charname 20 charsex intage floatscore charaddr 30 stu1 stu2 structstudent intnum charname 20 charsex intage floatscore charaddr 30 stu1 stu2 floatscore 例structdate intmonth intday intyear structstudent intnum charname 20 structdatebirthday stu 结构体变量的引用 11 3 结构体变量的引用除了赋值运算外 不能整体引用结构体变量 只能引用其成员 成员引用方式 结构体变量名 成员名 成员运算符 优先级最高 从左到右 对结构体变量的成员可以像普通变量一样进行各种运算 结构体变量的引用 11 3 结构体变量的引用不能将一个结构体变量作为一个整体进行输入输出 对结构体变量的输入输出应对其成员逐个进行 例1 结构体变量的输入输出 include 例1voidmain structstudent intnum charname 20 charsex intage floatscore charaddr 30 stu scanf d n 输入 1001 LiQiang M 18 80 200BeijingRoad 输出 1001LiQiangM1880 0200BeijingRoad 结构体变量的引用 11 3 结构体变量的引用可以将一个结构体变量的值赋值给另一个结构体变量 例structstudent intnum charname 20 charsex intage floatscore charaddr 30 stu1 stu2 stu2 stu1 结构体变量的引用 11 3 结构体变量的引用结构体嵌套时逐级引用 直至最低级 例structdate intmonth intday intyear structstudent intnum charname 20 structdatebirthday stu1 stu2 可以这样访问其成员 stu1 birthday year 不能用stu1 birthday来直接访问stu1变量中的成员birthday 因为stu1 birthday本身是一个结构体变量 结构体变量的初始化形式一 struct结构体名 类型名成员名 类型名成员名 struct结构体名结构体变量 初始数据 例structstudent intnum charname 20 charsex intage charaddr 30 structstudentstu1 112 WangLin M 19 200BeijingRoad 结构体变量的初始化 11 4 结构体变量的初始化形式二 struct结构体名 类型名成员名 类型名成员名 结构体变量 初始数据 例structstudent intnum charname 20 charsex intage charaddr 30 stu1 112 WangLin M 19 200BeijingRoad 结构体变量的初始化 11 4 结构体变量的初始化形式三 struct 类型名成员名 类型名成员名 结构体变量 初始数据 例struct intnum charname 20 charsex intage charaddr 30 stu1 112 WangLin M 19 200BeijingRoad 结构体变量的初始化 11 4 结构体变量的初始化 11 4 例11 1对结构体变量初始化 例11 1 includevoidmain structstudent longintnum charname 20 charsex charaddr 20 a 10101 LiLin M 123BeijingRoad 对结构体变量a赋初值 printf No ld nname s nsex c naddress s n a num a name a sex a addr 运行结果 No 10101name LiLinsex address 123BeijingRoad 结构体类型的应用 例2 输入平面上两个圆的圆心坐标和半径 判断两圆是包含 相交 不包含 相切 不包含 还是不交 分析 设两圆半径分别为r1 r2 d为两圆圆心间距离 则条件状态r1 r2d且d min r1 r2 d且d min r1 r2 max r1 r2 相交 不包含 include 例2 includevoidmain structpoint 表示平面上的点的类型 doublex y structcircle 表示圆的类型 doubleradius 表示半径structpointcenter 表示圆心 c1 c2 doubled sum max min inttype printf Entertheradiusandcenterofthefirstcircle 输入数据scanf lf lf lf 求两圆心间距离d sqrt pow c1 center x c2 center x 2 pow c1 center y c2 center y 2 sum c1 radius c2 radius 求两圆半径之和if c1 radius c2 radius max c1 radius min c2 radius else max c2 radius min c1 radius 求两圆半径较大者和较小者if sum d type 1 不交elseif sum d type 2 相切但不包含elseif d min max type 3 包含elsetype 4 相交但不包含switch type 输出结果 case1 printf nonintersecting n break case2 printf tangentbutnonincluded n break case3 printf included n break case4 printf intersectingbutnonincluded n break 结构体数组 11 5 结构体数组的定义 形式一 structstudent intnum charname 20 charsex intage structstudentstu 2 形式二 structstudent intnum charname 20 charsex intage stu 2 形式三 struct intnum charname 20 charsex intage stu 2 结构体数组初始化 分行初始化 struct intnum charname 20 charsex intage stu 100 WangLin M 20 101 LiGang M 19 110 LiuYan F 19 分行初始化 structstudent intnum charname 20 charsex intage stu 100 WangLin M 20 101 LiGang M 19 110 LiuYan F 19 结构体数组 11 5 顺序初始化 structstudent intnum charname 20 charsex intage structstudentstu 100 WangLin M 20 101 LiGang M 19 110 LiuYan F 19 structstudent intnum charname 20 charsex intage stu 3 stu 1 age strcpy stu 0 name ZhaoDa 结构体数组引用引用方式 结构体数组名 下标 成员名 结构体数组 11 5 结构体数组的每个元素都是一个结构体变量 使用规则如同一般的结构体变量 结构体数组 11 5 例11 2对候选人得票的统计程序 设有3个候选人 每次输入一个得票的候选人的名字 要求最后输出各人得票结果 include stdio h 例11 2程序1 include string h defineN100structperson charname 20 intcount leader 3 Li 0 Zhang 0 Fun 0 voidmain inti j charleader name 20 for i 1 i N 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 指向结构体类型数据的指针 11 6 指向结构体变量的指针一个结构体变量的指针就是该变量所占内存的起始地址 例11 3指向结构体变量的指针的应用 include 例11 3 includevoidmain structstudent longnum charname 20 charsex floatscore structstudentstu 1 structstudent p p stu 1 num 89101 strcpy stu 1 name LiLin stu 1 sex M stu 1 score 89 5 printf No ld nname s nsex c nscore f n stu 1 num stu 1 name stu 1 sex stu 1 score printf No ld nname s nsex c nscore f n p num p name p sex p score 运行结果 No 89101name LiLinsex score 89 500000No 89101name LiLinsex score 89 500000 不等于 p score 指向结构体类型数据的指针 11 6 指向结构体变量的指针引用结构体变量的成员的三种方法 设p是某结构体变量的指针 结构体变量 成员名 p 成员名p 成员名 p num p name p sex p score p num p name p sex p score 指向运算符 优先级最高 从左到右 指向结构体类型数据的指针 11 6 指向结构体变量的指针例3指向结构体变量的指针的应用 include 例3 includevoidmain structstudent longnum charname 20 charsex floatscore structstudentstu 1 structstudent p p stu 1 num 89101 strcpy stu 1 name LiLin stu 1 sex M stu 1 score 89 5 printf No ld nname s nsex c nscore f n stu 1 num stu 1 name stu 1 sex stu 1 score printf No ld nname s nsex c nscore f n p num p name p sex p score 指向结构体类型数据的指针 11 6 结构体数组与指针 请分析以下几种运算 设p指向结构体某个结构体变量 p np n p n p n p n p所指向的结构体变量的成员n p n p n 先引用p指向的结构体变量的成员n p再增值 p先增值 再引用p指向的结构体变量的成员n 指向结构体类型数据的指针 11 6 结构体数组与指针例11 4用指针法操作结构体数组 include 例11 4 程序1 structstudent intnum charname 20 charsex intage voidmain structstudentstu 3 10101 LiLin M 18 10102 ZhangFun M 19 10104 WangMing F 20 structstudent p printf No Namesexage n for p stu pnum p name p sex p age 运行结果 No Namesexage10101LiLinM1810102ZhangFunM1910104WangMingF20 include 例11 4 程序2 structstudent intnum charname 20 charsex intage voidmain structstudentstu 3 10101 LiLin M 18 10102 ZhangFun M 19 10104 WangMing F 20 structstudent p printf No Namesexage n for p stu pnum p name p sex p age include 例11 4 程序3 structstudent intnum charname 20 charsex intage voidmain structstudentstu 3 10101 LiLin M 18 10102 ZhangFun M 19 10104 WangMing F 20 structstudent p printf No Namesexage n for p stu pnum p name p sex p age 程序1较好 指向结构体类型数据的指针 11 6 注意 1 如果p的初值为stu 即指向第一个元素 则p加 后p就指向下一个元素 例如 p num先使p自加 再得到它指向的元素的num成员值 p num先得到p num的值 然后使p自加 指向stu 1 结构体数组 11 5 例11 2对候选人得票的统计程序 设有3个候选人 每次输入一个得票的候选人的名字 要求最后输出各人得票结果 include stdio h 例11 2程序2用指针法 include string h defineN100structperson charname 20 intcount voidmain inti j charleader name 20 structpersonleader 3 Li 0 Zhang 0 Fun 0 p for i 1 iname 0 p count for p leader i 0 iname p count 指向结构体类型数据的指针 11 6 用结构体变量和指向结构体的指针作函数参数将一个结构体变量的值传递给另一个函数的三种方法 用结构体变量的成员作实参用结构体变量作实参用指向结构体变量的指针作实参 将结构体变量的地址传给对应的形参 此法还可传结果 用结构体变量和指向结构体的指针作函数参数例11 5有一个结构体变量stu 内含学生学号 姓名和三门课的成绩 要求在main函数中为其赋值 在另一个函数print中输出其值 用结构体变量作函数参数 include 例11 5 include defineFORMAT d n s n f n f n f n structstudent intnum charname 20 floatscore 3 指向结构体类型数据的指针 11 6 voidmain voidprint structstudent structstudentstu stu num 12345 strcpy stu name LiLi stu score 0 67 5 stu score 1 89 stu score 2 78 6 print stu voidprint structstudentstu printf FORMAT stu num stu name stu score 0 stu score 1 stu score 2 printf n 运行结果 12345LiLi67 50000089 00000078 599998 结构体变量或指向结构体的指针作函数参数时 在函数外定义结构体类型 且需给出类型名供引用 注意 结构体变量数组成员的引用方法 指向结构体类型数据的指针 11 6 例11 6将例11 5改用指向结构体变量的指针作参数 include 例11 6 defineFORMAT d n s n f n f n f n structstudent intnum charname 20 floatscore 3 voidmain voidprint structstudent 形参类型修改成指向结构体的指针变量 structstudentstu 12345 LiLi 67 5 89 78 6 print main中对成员初始化也可改为用scanf输入 scanf d s f f f 输入 12345LiLi67 58978 6 运行结果 12345LiLi67 50000089 00000078 599998 指向结构体类型数据的指针 11 6 例4设一个学生的记录包括学号 姓名和成绩三项 将n个学生的记录按成绩从高到低的顺序排列 程序 程序 用指针处理链表 11 7 链表链表是一种基本且重要的数据结构 是动态地进行存储分配的一种结构 为何引进链表 数组存在的问题长度固定原因 静态存储分配插入删除操作不方便原因 数据连续存储链表的原理利用指针使得数据可以不连续存储 动态存储分配 用指针处理链表 11 7 线性链表的组成 结点 存放用户需要的实际数据和链接结点的指针 头指针 存放第一个结点的地址 即指向第一个结点 表末标志 空指针NULL 利用结构体建立链表 structstudent longnum floatscore structstudent next head 结点类型 头指针变量 用指针处理链表 11 7 例11 7建立一个简单链表 它由3个学生数据的结点组成 输出各结点中的数据 运行结果 1010189 51010390 01010785 0 voidmain 例11 7 structstudenta b c head p a num 10101 a score 89 5 建立三个结点b num 10103 b score 90 c num 10107 c score 85 head 循环至遇表末标记 includestructstudent longnum floatscore structstudent next 用指针处理链表 11 7 处理动态链表所需的函数提供动态地开辟和释放存储单元的有关库函数 malloc函数函数原型 void malloc unsignedintsize 作用 在内存的动态存储区中分配一个长度为size的连续空间 返回值 一个指向分配域起始地址的指针 类型为void 如果此函数未能成功地执行 内存空间不足 则返回空指针 NULL calloc函数函数原型 void calloc unsignedn unsignedsize 作用 在内存的动态存储区中分配 个长度为size的连续空间 返回值 一个指向分配域起始地址的指针 如果分配不成功 返回NULL 用指针处理链表 11 7 处理动态链表所需的函数calloc函数用calloc函数可以为一维数组开辟动态存储空间 n为数组元素个数 每个元素长度为Size free函数函数原型 voidfree void p 作用 释放由p指向的内存区 使这部分内存区能被其他变量使用 free函数无返回值 注意 使用以上库函数需包含头文件malloc h 用指针处理链表 11 7 建立动态链表基本思想 在程序执行过程中从空表开始建立一个链表 即一个一个地开辟结点和输入各结点数据 并建立起前后相链的关系 建立一个新结点的步骤 1 开辟新结点的存储空间 2 将新结点链接到链表中 3 向新结点输入数据 第2 3步顺序可对调 要点 新结点的链接 表头的处理 表末标记 用指针处理链表 11 7 例11 8写一函数建立一个存储若干学生数据的单向动态链表算法说明 约定学号不会为零 如果输入的学号为 则表示数据已输入完 初始化 初始化链表为空表 head NULL 指针变量 p1 指向新结点p2 指向新结点的前一个结点表头的处理 令头指针指向第一个建立的结点head p1 新结点的链接 第二个结点起 令前一个结点的指针成员p2 next p1 接着使p2指向新链入的结点p2 p1 数据结束的判断 当读入的学号p num等于0时 退出循环 表末标记的设置 p2 next NULL 图11 12 用指针处理链表 11 7 图11 13 图11 14 用指针处理链表 11 7 图11 15 图11 16 用指针处理链表 11 7 建立链表 include 例11 8 include defineLENsizeof structstudent structstudent longnum floatscore structstudent next voidmain structstudent creat voidprint structstudent head structstudent head head creat 建立链表print head 输出链表 structstudent creat 建立链表函数 structstudent head structstudent p1 p2 intn 0 num head NULL scanf ld voidprint structstudent head 输出链表函数 structstudent p printf nTherecordsinthelistare n p head while p NULL printf ld 5 1f n p num p score p p next 用指针处理链表 11 7 输出链表算法思想 定义一个指针变量p 通过头指针得到链表第一个结点的地址 p head 用指针变量p 从第一个结点起逐个指向链表的每个结点 输出其数据 指针移动操作 p p next 遇表末标记 则链表输出完毕 共用体 11 8 C语言的数据类型 共用体union 共用体 11 8 共用体用途 使几个不同类型的变量共占一段内存 相互覆盖 共用体类型定义 union共用体名 成员表列 例uniondata shorti charch floatf 类型定义不分配内存 形式一 uniondata shorti charch floatf a b 形式二 uniondata shorti charch floatf uniondataa b c p d 3 形式三 union shorti charch floatf a b c 共用体变量的定义 共用体变量定义分配内存 长度 最长成员所占字节数 共用体变量任何时刻只有一个成员存在 共用体 11 8 共用体 11 8 共用体变量引用引用方式 与结构体类似 共用体指针名 成员名 共用体变量名 成员名 共用体指针名 成员名 共用体 11 8 共用体变量引用引用规则不能整体引用共用体变量 只能引用其成员 共用体变量中起作用的成员是最后一次存储的成员 不能在定义共用体变量时初始化 可以用一个共用体变量为另一个变量赋值 共用体变量的地址和它的各成员的地址都是同一地址 如 a a i a ch a f都是同一个地址值 例union inti charch floatf a 1 a 1 5 例union inti charch floatf a b a i 1 a ch a a f 1 5 b a 例union inti charch floatf a a 1 例a i 1 a ch a a f 1 5 printf d a i 编译通过 运行结果不对 共用体 11 8 共用体变量引用说明可以使用指向共用体变量的指针 共用体与结构体可以互相嵌套 可以定义共用体数组 共用体 11 8 例11 12设有若干个人员的数据 其中有学生和教师 学生的数据中包括 姓名 号码 性别 职业 班级 教师的数据包括 姓名 号码 性别 职业 职务 可以看出 学生和教师所包含的数据是不同的 现要求把它们放在同一表格中 共用体 11 8 算法 读入position 共用体 11 8 voidmain inti n print

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论