C语言程序设计结构体与共用体ppt课件.ppt
《C语言程序设计结构体与共用体ppt课件.ppt》由会员分享,可在线阅读,更多相关《C语言程序设计结构体与共用体ppt课件.ppt(71页珍藏版)》请在三一办公上搜索。
1、第12章 结构体与共用体,程序设计语言,C,2,本章主要内容,结构体数据类型,共用体数据类型、枚举数据类型、定义数据类型的别名 结构体变量、结构体数组、结构体指针的定义和初始化 结构体成员的引用、成员选择运算符、指向运算符 向函数传递结构体变量、结构体数组、结构体指针 动态数据结构、动态链表,3,只能定义单一的数据类型,反映事物单一属性,如定义学生成绩:float score;,能定义复杂的数据类型,反映事物多个属性,存放相同数据类型的一组数据,如:float score30;,12.1 从基本数据类型到抽象数据类型,4,4,12.1从基本数据类型到抽象数据类型,用户自己构造数据类型复合数据类
2、型由基本数据类型迭代派生而来,表示复杂的数据对象典型的代表就是“结构体”抽象数据类型(Abstract Data Type,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是关键字,不能省略,合法标识符可省:无名结构体,成员类型可以是基本型或构造型,以分号;结尾,struct Student char no9;/学号 char name20;/姓名 char sex;/性别 unsigned int age;/年龄 float score;/成绩;,struct Date short year;/年
5、 short month;/月 short day;/日;,11,12.2.2 结构体类型定义,struct Student char no9;/学号 char name20;/姓名 char sex;/性别 short int age;/年龄 float score;/成绩;,结构体类型定义描述结构的组织形式,注意:结构体类型只是用户自定义的一种数据类型,用来定义描述结构的组织形式,不分配内存,只有用它来定义某个变量时,才会为该变量分配结构类型所需要大小的内存单元。,12,在结构体中数据类型相同的成员,既可逐个、逐行分别定义,也可合并成一行定义,就象一次定义多个变量一样。,struct St
6、udent_Info char no9;/学号 char name20;/姓名 char sex;/性别 unsigned int age;/年龄 unsigned int classno;/班级 float grade;/成绩;,struct Student_Info char no9,name20,sex;unsigned int age,classno;float grade;,12.2.2 结构体类型定义,13,12.2.3 结构体变量定义,struct 结构体类型名 数据类型名1 成员名1;数据类型名n 成员名n;struct 结构体类型名 变量列表;,先定义结构类型,再定义结构变量
7、,struct Student char no9;char name20;char sex;unsigned int age;float score;struct Student stu1,stu2;,定义一个类型为 struct student 结构体的变量,将会为该变量分配内存,大小是等于其所有成员变量的大小之和。sizeof(struct Student),14,12.2.3 结构体变量定义,定义结构体类型的同时定义结构体变量,struct 结构体类型名 数据类型名1 成员名1;数据类型名n 成员名n;变量名列表;,struct Studentchar no9;char name20;c
8、har sex;unsigned int age;float score;stu1,stu2;,struct char no9;char name20;char sex;unsigned int age;float score;stu1,stu2;,或,无名结构体定义,15,12.2.3 结构体变量定义,成员可以是结构体,结构体可以嵌套,struct dateint month;int day;int year;struct student int no;char name20;struct date birthday;stu;,struct student int no;char name2
9、0;struct date int month;int day;int year;birthday;stu;,16,12.2.4 结构体变量的初始化,定义结构体变量时给结构体成员赋值,注意:赋初值时,中间的数据顺序必须与结构体成员的定义顺序一致,否则就会出现混乱。,struct Student stu=“09122325,YangFan,M,19,90;,struct Student stu=19,YangFan,M,“09122325,90;,17,12.2.4 结构体变量的初始化,定义结构体变量时给结构体成员赋值,struct date int year,month,day;struct
10、Stu_Info char no9;/学号 char name20;/姓名 char sex;/性别 struct date birthday;/生日 float score;/成绩;,struct Stu_Info stu=20020306,ZhangMing,M,1986,12,10,90;,18,12.2.5 结构体变量的引用,引用规则:不能整体引用,只能引用变量的成员,引用方式:,结构体变量名.成员名,成员(分量)运算符结合性:从左向右,struct student char no9;char name20;char sex;unsigned int age;float score;s
11、tu1,stu2;,if(stu1=stu2).(),stu1.score=85.5;,stu1.age=stu2.age;,strcpy(stu1.no,“09122414”;),19,12.2.5 结构体变量的引用,可以将一个结构体变量赋值给另一个结构体变量结构体嵌套时逐级引用,struct student int no;char name20;struct date int month;int day;int year;birthday;stu1,stu2;,stu1.birthday.month=12;,结构体变量名.成员名.子成员名最低级子成员名,20,12.2.5 结构体变量的赋值
12、,strcpy(stu1.no,stu.no);strcpy(stu1.name,stu.name);stu1.sex=stu.sex;stu1.age=stu.age;stu1.score=stu.score;,struct Student stu;strcpy(stu.no,“09122424);strcpy(stu.name,“XuTeng);stu.sex=M;stu.age=21;stu.score=90;struct Student stu1;stu1=stu;,如果在定义结构体变量时并未对其赋初始值,那么在程序中要对它赋值的话,就只能一个一个地对其成员逐一赋值,或者用已赋值的同类
13、型的结构体变量对它赋值,21,12.2.5 结构体变量应用举例,【例】计算某个学生5门课的平均成绩,最高分和最低分,#include struct Student float score5;float avescore,maxscore,minscore;void main()int i;struct Student m;printf(input the score of five course:n);for(i=0;i 5;i+)/输入5门课的成绩 scanf(%f,m.avescore=0;m.maxscore=m.score0;m.minscore=m.score0;for(i=0;i
14、m.maxscore)m.maxscore=m.scorei;if(m.scorei m.minscore)m.minscore=m.scorei;m.avescore/=5;printf(“avescore=%4.1f,maxscore=%4.1f,minscore=%5.1fn,m.avescore,m.maxscore,m.minscore);,运行结果(设5门课的成绩为:75 80 86 90 68):avescore=79.8 maxscore=90.0 minscore=68.0,22,12.2.6 结构体数组,元素为结构体类型的数组称为结构体数组。在实际应用中,经常用结构体数组来
15、表示具有相同数据结构体的一个群体。例如一个班的学员档案,一个公司的职工档案等。,结构体数组的定义,形式一:struct Student char no9,name20,sex;unsigned int age;float score;struct Student stu10;,形式二:struct Student char no9,name20,sex;unsigned int age;float score;stu10;,形式三:struct char no9,name20,sex;unsigned int age;float score;stu10;,23,12.2.6 结构体数组与二维表
16、的对应关系,结构体数组就相当于一张二维表,表的框架对应的就是某种结构体类型,表中的每一列对应该结构体的成员,表中每一行信息对应该结构体数组元素各成员的具体值,表中的行数对应结构体数组的大小。,结构体类型Student,struct Student char no9;char name20;char sex;unsigned int age;float score;stu10;,24,12.2.6 结构体数组的初始化,基本格式:struct 结构体类型 数组size=初值表1,初值表n;,例:,全部初始化时维数可省,25,12.2.6 结构体数组的引用,引用格式:结构体数组名下标.成员名;,st
17、ruct Student char no9;char name20;char sex;unsigned int age;float score;stu10;,strcpy(stu0.name,WangFei);,stu1.age+;,printf(%s,stu0.name);,26,12.2.6 结构体数组举例,#include#include struct person char name20;/候选人姓名 int count;/得票数 leader3=Li,0,Zhang,0,Wang,0;,void main()int i,j;char leader_name20;printf(inpu
18、t name:n);while(1)/统计候选人得票数 gets(leader_name);/输入候选人姓名 if(strcmp(leader_name,0)=0)/输入为0结束 break;for(j=0;j 3;j+)/比较是否为合法候选人 if(strcmp(leader_name,leaderj.name)=0)/合法 leaderj.count+;/得票数加1 for(i=0;i 3;i+)/显示候选人得票数 printf(%5s:%dn,leaderi.name,leaderi.count);,【例】统计侯选人选票(输入0结束),27,12.2.6 结构体数组举例,【例12.3】利
19、用结构体数组计算每个学生的平均分,28,29,12.2.7 结构体指针,定义:struct 结构体类型名*指针变量名;,struct Student stu;struct Student*p=,结构体指针的引用:指针变量名-成员名 或(*指针变量名).成员名,stu.age=18;(*p).age=18;p-age=18;,30,12.2.7 指向结构体类型数据的指针,#include#include struct Studentlong int num;char name20;char sex;float score;void main()struct Student stu_1;struc
20、t Student*p;p=,31,12.2.7 指向结构体数组的指针,#include struct Studentlong int num;char name20;char sex;int age;void main()struct Student*p;struct Student stu=10101,李林,M,18,10102,张奋,M,19,10103,王敏,F,20;printf(学号 姓名 性别 年龄n);for(p=stu;p num,p-name,p-sex,p-age);,32,12.2.7 指向结构体数组的指针,p=stu,p+1,p+2,stu0,stu1,stu2,33
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言程序设计 结构 共用 ppt 课件
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-2052478.html