第05章结构体与共用体-枚举.ppt

上传人:本田雅阁 文档编号:2250527 上传时间:2019-03-11 格式:PPT 页数:38 大小:1.13MB
返回 下载 相关 举报
第05章结构体与共用体-枚举.ppt_第1页
第1页 / 共38页
第05章结构体与共用体-枚举.ppt_第2页
第2页 / 共38页
第05章结构体与共用体-枚举.ppt_第3页
第3页 / 共38页
亲,该文档总共38页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第05章结构体与共用体-枚举.ppt》由会员分享,可在线阅读,更多相关《第05章结构体与共用体-枚举.ppt(38页珍藏版)》请在三一文库上搜索。

1、第5章 结构体与共用体,2,本章主要内容,结构体类型与结构体变量,结构体数组,结构体变量与函数*,共用体,枚举类型,typedef定义类型,结构体变量与指针*,3,5.1 结构体定义、引用及赋值,int num; char name20; char sex; int age; float score; char address50;,struct student int num; char name20; char sex; int age; float score; char address50; ;,(1)结构体定义,struct student int num; char name20;

2、 char sex; int age; float score; char address50; ;,结构体类型名,结构体各成员,定义结构体类型大括号外要加分号,struct student stu1,stu2;,系统不为结构体 类型的定义开辟 内存单元,只当 定义了结构体变 量后才会为变量 分配相应大小的 内存单元。,stu1和stu2在 内存占几个字节?,先声明结构体类型再定义结构体变量 struct student int num; char name20; char sex; int age; float score; char address50; ; struct student

3、stu1,stu2;,声明结构体类型的同时定义结构体变量 struct student int num; char name20; char sex; int age; float score; char address50; stu1,stu2;,直接定义结构体变量 struct int num; char name20; char sex; int age; float score; char address50; stu1,stu2;,这种不带结构体类型 名的定义,是无法重 用的。,说明: 类型与变量名是不同的概念; 对结构体的成员的使用与普通变量类似; 结构体的成员也可以是另一个结构体

4、; struct student int num; char name20; char sex; struct date birthday; float score; char address50; ; 成员名可以与程序中的其它变量同名。,struct date int year; int month; int day; birthday;,(2)结构体变量的引用 struct student int num; char name20; char sex; int age; fl oat score; char address50; stu1,stu2; stu1.num=10021; str

5、cpy(stu1.name, “Xiao hua“); stu1.age=18; stu1.score=85.5 ,结构体变量的引用: 结构体变量名.成员名,结构体变量的成员在引 用时和其同类型的普通 变量引用方法一致。,struct student int num; char name20; char sex; struct date int year; int month; int day; birthday; float score; char address50; stu; stu.birthday.year=2000; ,结构体变量的成员 又是结构体时,应 一层层找到最低一级的成员。

6、,(3)结构体变量的初始化 变量初始化具体形式: struct 结构体类型名 类型说明符1 成员名1; 类型说明符2 成员名2; 类型说明符3 成员名3; 变量名列表初始化数据;,(3)结构体变量的初始化 例5-1对结构体变量初始化。 void main() struct student /*定义结构体*/ int num; char name25; char sex; int age; float score; char addr35; student2,student1=102, “hang ping“, M, 18,85.5, “shanghai“; student2=student1;

7、 printf(“Number=%dnName=%sn“,student2.num, student2.names); printf(“Sex=%cnScore=%fn,student2.sex“, student.scores); ,13,5.2 结构体数组,struct student int num; char name20; char sex; int age; float score; char address50; stu3;,struct student int num; char name20; char sex; int age; float score; char addr

8、ess50; stu;,a.结构体数组定义,14,结构体数组示例,b.结构体数组初始化 struct student int num; char name20; char sex; int age; float score; char address50; stu3=1001,“Li Nin“, M, 14, 79, “103 Beijing Road“ ,1002, , “Zhang Fan“, M, 15, 80, “46 Tangshan Road“ ,1003, “Wang Ying“, F, 14, 86, “10 Zhongshan Road“ ;,15,例5.2由键盘输入学生信息

9、,并将其输出。 #include void main() struct STUD char name20; long num; int age; char sex; float score; stud3; int i; for(i=0;i3;i+) printf(“Input all information about the No.%d student:n“,i+1); gets(studi.name); scanf(“%ld,%d,%c,%f“, ,16,5.6指向结构体类型数据的指针,例5.3 指向结构体变量的指针 #include void main() struct student

10、long num; char name20; char sex; float score; stu,*p= ,结构体变量的引用: 结构体变量名.成员名 指针变量名-成员名 (*指针变量名).成员名,5.3结构体变量与函数*,例5-3 将键盘输入的有关学生的档案信息用函数list实现。 #include struct STUD char name20; long num; int age; char sex; float score; ; void list(struct STUD stud) printf(“%-20s%-10ld%-4d%-4c%-6.2fn“,stud.name,stud.

11、num,stud.age, stud.sex,stud.score); void main( ) struct STUD stud3; int i; for(i=0;i3;i+) printf(“Input all information about the No.%d student:n“,i+1); getchar(); gets(studi.name); scanf(“%ld,%d,%c,%f“, ,定义结构体变量指针的一般形式是: struct 结构体类型名 *结构体指针变量名 例5-5 指向结构体变量的指针应用。 #include “stdio.h“ #include “stdlib

12、.h“ void main( ) struct STUD char name20; long num; int age; float score; ; struct STUD stud1; struct STUD *p; p= ,19,5.4 结构体变量及其指针*,例5-6指向结构体数组的指针的应用。 #include “stdio.h“ struct STUD int num; char *name; char sex; float score; boy5= 101,“Zhou ping“,M,45, 102,“Zhang ping“,M,62.5, 103,“Liou fang“,F,92

13、.5, 104,“Cheng ling“,F,87, 105,“Wang ming“,M,58 ; void main( ) struct STUD *p; printf(“n%-20s%-10s%-4s%-6sn“,“No.“,“Name“,“Sex“,“Scores“); for(p=boy;pnum,p-name,p-sex,p-score); ,20,结构体变量和指向结构体的指针作函数参数,有三种方法可以将结构体变量的值传递给一个函数使用: (1) 将结构体变量的成员作为函数的实参,将实参值传给形参。用法和用普通变量作实参是一样的,属于“值传递”方式。应当注意实参与形参的类型保持一致。

14、 (2) 用结构体变量作实参。用结构体变量作实参时,采取的也是“值传递”的方式,将结构体变量所占的内存单元的内容全部顺序传递给形参,形参也必须是同类型的结构体变量。这种传递方式较耗费系统内存和外存,这种方式一般较少使用。 (3) 将指向结构体变量(或数组)的指针作函数实参,将结构体变量(或数组)的地址传给形参。,21,例5-7 有一个结构体变量stud,其中包含学生学号、姓名和3门课程的成绩。要求在主函数中赋值,在另一个自定义函数print中将它们输出。 #include “stdio.h“ #include “string.h“ #define FORMAT “%dn%sn%fn%fn%fn

15、“ struct STUDENT int num; char name20; float scores3; ; void print(struct STUDENT stud) printf(FORMAT,stud.num,stud.name,stud.scores0,stud.scores1, stud.scores2); printf(“n“); void main( ) struct STUDENT stu; stu.num = 10331; strcpy(stu.name,“Wei Fang“); stu.scores0 = 85.4; stu.scores1 = 78.9; stu.s

16、cores2 = 69.4; print(stu); ,课堂练习,1.有如下定义语句: struct long num; char name10; float score3; s2=101L, “zhao“, 90, 80, 70, 102L, “tang“, 75, 65, 58,*p=s; 则表达式(*(p+1).name1的值是?表达式(p+1)-score1的值是?,23,课堂练习,2.设有如下定义语句: struct int x5; int k; s=1,2,3,*p= 那么表达式p1-x1的值是?表达式(*p1).k的值是?,24,课堂练习,3. 写出程序运行结果: struct

17、stu int x; int *px; a4,*p=a; void main() int i,y4=10,20,30,40; for(i=0;ix); printf(“%dn“,(+p)-x); printf(“%dn“,+(*p-px); ,25,5.8 共用体,学生的信息:,老师的信息:,使几个不同的变量共占同一段内存的结构称为共用体类型的结构。 定义共用体类型变量的一般形式为: union 共用体名 成员表列 变量表列; struct stuTeacher int num; char name20; char addr50; char tel8; char type; union inf

18、o char class10; char job20; ; ,1.共用体类型的定义,例如: union data union data int i; int i; char ch; 或 char ch; float f; float f; a,b,c; ; union data a,b,c;,5.2 共用体变量的定义和引用 对于共用体变量的定义: union data int i; char ch; float f; a; 成员的引用: a.i a.ch a.f,共用体类型的特点 void main( ) union data int i; char ch; float f; a; a.ch=

19、65; a.i=28; printf(“i=%dn“,a.i); printf(“ch=%cn“,a.ch); printf(“f=%4.1fn“,a.f); ,对共用体中的i成员赋值,之前对成员ch赋值不再起作用,此时仅成员i起作用,其余成员不起作用。,共用体变量和其各成员的地址均是一样的。对共用体变量不能初始化,不能对共用体变量名赋值。,5. 枚举类型,一个星期有7天,分别是: 星期1,星期2,星期3,星期4,星期5,星期6,星期天 枚举:将变量所有可能值列举出来,变量的值只限于列举出来的值的范围内。 enum weekday sun, mon, tue, wed, thu, fri, s

20、at; enum weekday workday, week_end; enum weekday sun=7,mon=1,wed,thu,fri,sat;,31,枚举类型定义, 枚举类型名为 enum weekday,枚举元素,枚举常量 枚举元素按常量处 理,故不能对其赋 值。其值是按其定 义时的顺序,分别 为0,1,2,3. 定义中,sun=0, mon=1,tue=2.,枚举变量定义,enum weekday sun,mon,tue,wed,thu,fri,sat; enum weekday workday,weekend; workday=mon; workday=1; workday=

21、(enum weekday)1; workday=mon; workday=(enum weekday)(7-3);,5. 用户自定义类型,typedef int INTEGER; typedef long double LD; typedef struct int month; int day; int year; DATE; DATE birthday; DATE *p; typedef int NUM100; NUM n;,33,声明一个新类型名方法: 先按定义变量的方法写出定义体 将变量名换新类型名 在最前面加上typedef 然后可用新类型名去定义变量,typedef 说明: 用ty

22、pedef可声明各种类型名,但不能用来定义变量 用typedef只是对已经存在的类型增加一个类型名(“取别名”); typedef与#define有相似之处; 当不同源文件中用到同一类型数据时,常用typedef声明一些数据类型,把它们单独放在一个文件中,然后在需要用到它们的文件中用#include命令把它们包含进来; 使用typedef有利于程序的通用与移植。,. 程序设计举例,例5-11 已知某年的元旦是星期几,打印该年某一月份的日历表 #include “stdio.h“ typedef struct int year,mon,day; enum weekday nun,mon,tue,

23、wed,thu,fri,satweek; daily; void main( ) daily days; printf(“Which year?“); scanf(“%d“, ,35,montable(daily d) int i, s, ds,t; daily md; md.year = d.year; md.day = 1; printf(“Which month?“); scanf(“%d“, /*计算当月前几个月的总天数*/,36,md.week = (s + d.week)%7; /* 计算该月1号是星期几,同时计算该月天数存入ds中*/ printf(“t=%4d Year, %2

24、d Month =n“,md.year,md.mon); printf(“n“); printf(“%5s%5s%5s%5s%5s%5s%5sn“, “Sun“,“Mon“,“Tue“, “Wed“,“Thu“,“Fri“,“Sat“); printf(“n“); for(i = 0; imd.week * 5; i+) printf(“ “); /* 计算该月1号的打印位置 */ for(i = 1; i=ds; i+) printf(“%5d“,i); if(+md.week = 7) md.week = 0; printf(“n“); /* 超过一周换行打印*/ if(md.week != 0) printf(“n“); printf(“n“); ,37,本章小结,结构体 共用体 枚举类型 typedef定义类型,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1