结构体与共用体-枚举.ppt_第1页
结构体与共用体-枚举.ppt_第2页
结构体与共用体-枚举.ppt_第3页
结构体与共用体-枚举.ppt_第4页
结构体与共用体-枚举.ppt_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

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

文档简介

第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; 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 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;,这种不带结构体类型 名的定义,是无法重 用的。,说明: 类型与变量名是不同的概念; 对结构体的成员的使用与普通变量类似; 结构体的成员也可以是另一个结构体; 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; strcpy(, “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; ,结构体变量的成员 又是结构体时,应 一层层找到最低一级的成员。,(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; printf(“Number=%dnName=%sn“,student2.num, s); 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 address50; 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由键盘输入学生信息,并将其输出。 #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(); scanf(“%ld,%d,%c,%f“, ,16,5.6指向结构体类型数据的指针,例5.3 指向结构体变量的指针 #include void main() struct student 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.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(); scanf(“%ld,%d,%c,%f“, ,定义结构体变量指针的一般形式是: struct 结构体类型名 *结构体指针变量名 例5-5 指向结构体变量的指针应用。 #include “stdio.h“ #include “stdlib.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.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) 将结构体变量的成员作为函数的实参,将实参值传给形参。用法和用普通变量作实参是一样的,属于“值传递”方式。应当注意实参与形参的类型保持一致。 (2) 用结构体变量作实参。用结构体变量作实参时,采取的也是“值传递”的方式,将结构体变量所占的内存单元的内容全部顺序传递给形参,形参也必须是同类型的结构体变量。这种传递方式较耗费系统内存和外存,这种方式一般较少使用。 (3) 将指向结构体变量(或数组)的指针作函数实参,将结构体变量(或数组)的地址传给形参。,21,例5-7 有一个结构体变量stud,其中包含学生学号、姓名和3门课程的成绩。要求在主函数中赋值,在另一个自定义函数print中将它们输出。 #include “stdio.h“ #include “string.h“ #define FORMAT “%dn%sn%fn%fn%fn“ struct STUDENT int num; char name20; float scores3; ; void print(struct STUDENT stud) printf(FORMAT,stud.num,,stud.scores0,stud.scores1, stud.scores2); printf(“n“); void main( ) struct STUDENT stu; stu.num = 10331; strcpy(,“Wei Fang“); stu.scores0 = 85.4; stu.scores1 = 78.9; stu.scores2 = 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 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 info 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=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, sat; 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=(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 说明: 用typedef可声明各种类型名,但不能用来定义变量 用typedef只是对已经存在的类型增加一个类型名(“取别名”); typedef与#define有相似之处; 当不同源文件中用到同一类型数据时,常用typedef声明一些数据类型,把它们单独放在一个文件中,然后在需要用到它们的文件中用#include命令把它们包含进来; 使用typedef有利于程序的通用与移植。,. 程序设计举例,例5-11 已知某年的元旦是星期几,打印该年某一月份的日历表 #include “stdio.

温馨提示

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

评论

0/150

提交评论