《第09章结构体与共用体.ppt》由会员分享,可在线阅读,更多相关《第09章结构体与共用体.ppt(33页珍藏版)》请在三一文库上搜索。
1、第九章 结构体与共用体,第九章 结构体与共用体,第九章 结构体与共用体, 结构体类型及其变量定义、引用和初始化 结构体数组和指向结构体数组的指针 链表处理 共用体和枚举类型,本章重点:,9.1 概述,一、C语言的数据类型,1、基本类型,int float double char 数组 指针,第九章 结构体与共用体,2、自定义类型,C语言还允许自定义类型 自定义类型可以看成是对系统类型的扩充 C语言中自定义类型中最常用的是结构体类型,第九章 结构体与共用体,main() struct point int x; int y; ; struct point p1, p2; p1.x=10; p1.y
2、=10; p2.x=20; p2.y=20; printf(“p1 : (%d,%d)n”, p1.x, p1.y); printf(“p2 : (%d,%d)n”, p2.x, p2.y); getch(); ,读下面程序, 思考p1, p2是什么类型的变量,第九章 结构体与共用体,二、结构体类型的定义,1、格式,struct 结构类型名 数据类型 数据域1; 数据类型 数据域2; 数据类型 数据域; ;,如:struct complex_number int x; int y; ;,第九章 结构体与共用体,2、说明,1)结构体名即自定义类型名 2)成员和域的概念 3)注意不要丢了分号,第九
3、章 结构体与共用体,9.2 定义结构体类型变量的方法,main() struct complex_number int x; int y; ; struct complex_number c1, c2, c3; c1.x=10; c1.y=10; c2.x=20; c2.y=20; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; printf( “ c3=%d+%d i ”, c3.x, c3.y); getch(); ,第九章 结构体与共用体,先定义结构体类型,再定义结构体类型变量 注意格式,不能少了struct 请正确区分结构体类型和结构体类型的变量两个概念 结构体类型中的
4、成员变量也可以是结构体类型的,几点说明,第九章 结构体与共用体,9.3 结构体变量的初始化和引用,main() struct complex_number int x; int y; ; struct complex_number c1=10,10, c2=20,20; struct complex_number c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; printf( “ c3=%d+%d i ”, c3.x, c3.y); getch(); ,第九章 结构体与共用体,1. 和其它变量一样,可以在定义时对结构体变量初始化 2. 下面的写法是错误的(请与初始化比较
5、) struct point p1=1,2, p2; p2= 3, 42 ; 3. p2=p1是正确的,就象int x=3, y; y=x一样 4. 整体输入输出、运算也是错误的 5. 可以引用成员的地址,也可以引用结构体变量的地址,如 printf(“%o”, 请思考,一个结构体类型的变量在内存中占中字节?,几点说明,第九章 结构体与共用体,9.4 结构体数组,一、结构体数组的定义,main() int i; struct student int num; char *name; int age; ; struct student s3; clrscr();,第九章 结构体与共用体,for
6、(i=0; i3; i+) si.num=i; si.name=“boy”+i; si.age=20+i; for (i=0; i3; i+) printf(“%d ”, si.num); printf(“%s ”, si.name); printf(“%dn”, si.age); getch(); ,第九章 结构体与共用体,几点说明,1)结构体数组的数组名也是数组的首地址,指向第一个元素 2)试思考结构体数组在内存中的存储方式,及s+1的含义,第九章 结构体与共用体,二、结构体数组的初始化,main() int i; struct student int num; char name20;
7、int age; ; struct student s3 = 1,“boy1”,16, 2,“boy2”,17, 3,“boy3”,18 ;,第九章 结构体与共用体,clrscr(); for (i=0; i3; i+) printf(“%d ”, si.num); printf(“%s ”, si.name); printf(“%dn”, si.age); getch(); ,第九章 结构体与共用体,三、结构体数组的应用举例,例1 三个学生他们的学号、姓名、及其每个学生四门课程的成 绩如下:20, “XYZ”,66,57,70,86, 36, “LKH”, 58, 67, 90,45, 45
8、, “ZSF”,98,67,87,95, 编程输入一 个学生的学号,输出该学生的完整信息。,main() int i, n, f=-1; struct stu int num; char *name; int sc1; int sc2; int sc3; int sc4; ;,第九章 结构体与共用体,struct stu s3= 20, “XYZ”,66,57,70,86, 36, “LKH”,58,67,90,45, 45, “ZSF”,98,67,87,95, ; clrscr(); printf(“please input n:”, n); scanf(“%d”, ,第九章 结构体与共用
9、体,printf(“%d %s ”, sf.num, sf.name); printf(“%d %d ”, sf.sc1, sf.sc2); printf(“%d %d ”, sf.sc3, sf.sc4); getch(); ,思考如何编程输出每个同学的总分,第九章 结构体与共用体,9.5 指向结构体的指针,main() int i; struct student int num; char name20; int age; ; struct student s3 = 1,“boy1”,16, 2,“boy2”,17, 3,“boy3”,18 ;,第九章 结构体与共用体,struct stu
10、dent *p=s; clrscr(); for (i=0; i3; i+) printf(“%d ”, (*p).num); printf(“%s ”, (*p).name); printf(“%dn”, (*p).age); p+; getch(); ,第九章 结构体与共用体,9.6 链表,上述的 例3 中出现了链表,同数组一样,链表也可以用来存储一组数。不同在于:数组是顺序存储,链表链式存储。链表用起来比较麻烦,但在某些情况下无法使用数组,而必须使用链表,还有些情况下,使用链表比使用数组要方便。,先看下面三个例子,一、链表的概念,二、几个函数说明,1、malloc()函数 2、callo
11、c()函数 3、free()函数 4、sizeof(strcut node)的含义,第九章 结构体与共用体,例1 输入10个整数,把它们倒序输出,main() int a10, I; for (I=0; I=0; I-) printf(“%d ”, aI); getch(); ,第九章 结构体与共用体,例2 输入n个整数,把它们倒序输出,n的值由键盘输入,main() int n, I, *a; printf(“please input n: ”); scanf(“”, ,第九章 结构体与共用体,例3 任意输入若干个整数,把它们输出,#define NULL 0 #define LEN siz
12、eof(struct node) struct node int x; struct node *next; ; main() int t; struct node *head, *p, *q; clrscr(); printf(“please input t: ”); scanf(“%d”, if (t=0) ,第九章 结构体与共用体,printf(“end”); getch(); exit(0); head=p=q=(struct node *)malloc(LEN); do q-x=t; printf(“please input t: ”); scanf(“%d”, ,第九章 结构体与共
13、用体,三、链表的建立,#define NULL 0 #define LEN sizeof(struct node) struct node int x; struct node *next; ; main() struct node *head; clrscr(); head=create(); list(head); ,struct node *create() int t; struct node *head=NULL; printf(“please input t (0=end) : “); scanf(“%d“, ,第九章 结构体与共用体,while (1) head=insert(h
14、ead,t); printf(“please input t (0=end) : “); scanf(“%d“, ,第九章 结构体与共用体,第九章 结构体与共用体,四、链表的输出和释放,void list(struct node *head) struct node *p; for (p=head; p!=NULL; p=p-next) printf(“%d “, p-x); ,第九章 结构体与共用体,void freeall(struct node *head) struct node *p1, *p2; p1=p2=head; while (p1!=NULL) p1=p1-next; fr
15、ee(p2); p2=p1; ,第九章 结构体与共用体,五、链表中节点的插入和删除,第九章 结构体与共用体,9.7 共用体,一、概念 使几个不同的变量占用同一段内存空间的结构称为共用体。 二、共用体类型的定义 共用体类型的定义与结构体类型的定义类似 union 共用体类型名 成员列表; ; 三、共用体变量的定义 四、共用体变量的引用 共用体变量的引用与结构体类型变量一样,也只能逐个引用共用 体变量的成员例如,访问共用体变量d1各成员的格式为:d1.i、 d1.ch、d1.f,。,第九章 结构体与共用体,9.8 枚举类型,一、枚举类型的定义 enum 枚举类型名 取值表 ; 例如,enum weekdays Sun,Mon,Tue,Wed,Thu,Fri,Sat;,二、枚举变量的定义 枚举变量的定义与结构变量类似,第九章 结构体与共用体,9.9 用typedef定义类型,可使用typedef定义已有类型的别名。该别名与标准类型名一样,可用来定义相应的变量。,例1 给实型float定义1个别名REAL。 typedef float REAL; REAL f;,例2 给如下所示的结构类型struct date定义1个别名DATE。 struct date int year, month, day; ; typedef struct data DATE;,
链接地址:https://www.31doc.com/p-3417918.html