第05章结构体与共用体-枚举.ppt
《第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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 05 结构 共用 枚举
链接地址:https://www.31doc.com/p-3417596.html