1 / 109

第八章 结构和联合类型

第八章 结构和联合类型. 教 材 : C 程序设计导论. 主讲:谭成予 nadinetan@163.com. 武汉大学计算机学院. 结构类型定义与引用. 结构类型和指针. 结构数组. 链表. 联合类型. 本讲重点. 结构类型. 结构类型是 一种 构造数据类型 用途:把 不同类型 的数据组合成一个整体------- 自定义数据类型. 合法标识符 可省 : 无名结构类型. struct [ 结构名 ] { 类型标识符 成员名; 类型标识符 成员名; ……………. } ;. 成员类型可以是 基本型或构造型

tavita
Download Presentation

第八章 结构和联合类型

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第八章 结构和联合类型 教 材: C程序设计导论 主讲:谭成予 nadinetan@163.com 武汉大学计算机学院

  2. 结构类型定义与引用 结构类型和指针 结构数组 链表 联合类型 本讲重点

  3. 结构类型 • 结构类型是一种构造数据类型 • 用途:把不同类型的数据组合成一个整体-------自定义数据类型 合法标识符 可省:无名结构类型 struct [结构名] { 类型标识符 成员名; 类型标识符 成员名; ……………. }; 成员类型可以是 基本型或构造型 (数组、指针或其 他结构类型) struct是关键字, 不能省略

  4. 4字节 order … name 20字节 1字节 sex 2字节 age ….. 20字节 score ….. addr 30字节 结构类型 例 struct student { long int order; char name[20]; char sex; short int age; int score[10]; char addr[30]; }; 结构类型定义的作用域 结构类型定义描述结构 的组织形式,不分配内存

  5. 结构类型 例 struct id_card { char name[30]; char sex; char nationality[20]; struct date { int year,month,day; }birthday; char *p_addr; struct date signed_date; long number; char *office; }; 结构类型可以嵌套定义 同一结构类型各成员不能同名,不同结构类型成员可以同名

  6. 结构类型 例 struct wrong { char name[30]; int count; struct wrong a; }; × 结构类型不能递归定义

  7. 结构变量的定义 先定义结构类型,再定义结构变量 一般形式: struct 结构名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }; struct 结构名 变量名表列;

  8. 结构变量的定义 例 struct student { long int order; char name[20]; char sex; short int age; int score[10]; char addr[30]; }; struct student stu1,stu2; /*struct coord表示屏幕上一个点的坐标*/ 例 struct coord{ float x; float y; }; struct coord first,second;

  9. 结构变量的定义 例 #define STUDENTstruct student STUDENT { long int order; char name[20]; char sex; short int age; int score[10]; char addr[30]; }; STUDENT stu1,stu2;

  10. 结构变量的定义 定义结构类型的同时定义结构变量 一般形式: struct 结构名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }变量名表列;

  11. 结构变量的定义 例 struct student { long int order; char name[20]; char sex; short int age; int score[10]; char addr[30]; }stu1,stu2; 例 struct coord{ float x; float y; }first,second; /*struct coord表示屏幕上一个点的坐标*/

  12. 一般形式: struct { 类型标识符 成员名; 类型标识符 成员名; ……………. }变量名表列; 用无名结构类型直接定义 变量只能一次 结构变量的定义 直接定义结构类型变量

  13. 结构变量的定义 例 struct { long int order; char name[20]; char sex; short int age; int score[10]; char addr[30]; }stu1,stu2; 例 struct { float x; float y; }first,second; /*struct coord表示屏幕上一个点的坐标*/

  14. 结构变量的定义 • 说明 • 结构类型与结构类型变量概念不同 • 类型:不分配内存; 变量:分配内存 • 类型:不能赋值、存取、运算; 变量:可以 • 结构类型可嵌套 • 结构类型成员名与程序中变量名可相同,不会混淆 • 结构类型及变量的作用域与生存期

  15. 例 struct date { int month; int day; int year; }; struct student { int num; char name[20]; struct date birthday; }stu; birthday num name month day year 结构变量的定义

  16. 例 struct student { int num; char name[20]; struct date { int month; int day; int year; }birthday; }stu; birthday num name month day year 结构变量的定义

  17. 结构类型变量的引用 • 引用规则 • 结构类型变量不能整体引用,只能引用变量成员 • 引用方式:结构类型变量名.成员名 • 可以将一个结构类型变量赋值给另一个结构类型变量 • 结构类型嵌套时逐级引用

  18. 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; stu1.num=10; stu1.score=85.5; stu1.score+=stu2.score; stu1.age++; 结构类型变量的引用 结构类型变量不能整体引用,只能引用变量成员 引用方式: 结构类型变量名.成员名 成员(分量)运算符 优先级: 1 结合性:从左向右

  19. 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; printf(“%d,%s,%c,%d,%f,%s\n”,stu1); () stu1={101,“Wan Lin”,‘M’,19,87.5,“DaLian”}; () 结构类型变量的引用 不能整体引用

  20. 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; if(stu1==stu2) …….. () 结构类型变量的引用 不能整体引用

  21. 例 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; stu2=stu1; ( ) 结构类型变量的引用 结构类型嵌套时逐级引用

  22. birthday num name month day year 结构类型变量的引用 结构类型嵌套时逐级引用 例 struct student { int num; char name[20]; struct date { int month; int day; int year; }birthday; }stu1,stu2; stu1.birthday.month=12;

  23. /*L8-1.C: 计算某个同学5门课程成绩的平均分。*/ #include <stdio.h> int main(void) { struct student{ char *name; /*姓名*/ long order; /*学号*/ int score[5]; /*成绩*/ float average; /*平均分*/ }who; int sum=0,n; printf(“input name,order and 5 scores\n”); scanf(“%s%ld”,who.name,&who.order); char name[20];

  24. for(n=0;n<5;n++) scanf(“%d”,&who.score[n]); who.average=0.0; for(n=0;n<5;n++) sum+=who.score[n]; who.average=(float)sum/5; printf(“\nname=%s\torder=%ld\n”,who.name.who.order); printf(“average=%f\n”,who.average); return 0; }

  25. /*L8-2.C: 输入矩形左上角和右下角坐标,计算该矩形长和宽及面积。*/ #include <stdio.h> #include <math.h> int main(void) { float length,width,area; struct coord{ float x,y; }; struct rectangle{ struct coord topleft,bottomrt;}mybox; printf(“enter the top left x,y coordinate:\n”); scanf(“%f%f”,&mybox.topleft.x,&mybox.topleft.y); printf(“enter the bottom right x,y coordinate:\n”); scanf(“%f%f”,&mybox.bottomrt.x,&mybox.bottomrt.y); length=fabs((double)(mybox.bottomrt.x-bybox.topleft.x)); width=fabs((double)(mybox.topleft.y-mybox.bottomrt.y)); area=length*width; printf(“\nlength=%f\twidth=%f\n”,length,width); printf(“area=%f\n”,area); return 0; }

  26. 结构变量的初始化 形式一: struct 结构类型名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }; struct 结构类型名 结构类型变量={初始数据};

  27. 结构变量的初始化 例 struct student { int num; char name[20]; char sex; int age; char addr[30]; }; struct student stu1={112,“Wang Lin”,‘M’,19, “200 Beijing Road”};

  28. 结构变量的初始化 例 struct student { char name[20]; long order; int score[5]; float average; }; struct student who={“Wang Lin”,031112,{92,91,89,87,94},0.0};

  29. 结构变量的初始化 例 struct coord { float x,y;}; struct rectangle{ struct coord topleft; struct coord bottomrt; }; struct rectangle mybox={{1.8,8.3},{12.4,1.29}};

  30. 结构变量的初始化 形式二: struct 结构类型名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }结构类型变量={初始数据};

  31. 结构变量的初始化 例 struct student { int num; char name[20]; char sex; int age; char addr[30]; }stu1={112,“Wang Lin”,‘M’,19, “200 Beijing Road”}; 初值表中初值的个数<=结构变量长远的个数

  32. 结构变量的初始化 例 struct student { char name[20]; long order; int score[5]; float average; }who={“Wang Lin”,031112,{92,91,89,87,94},0.0}; 例 struct coord { float x,y;}; struct rectangle{ struct coord topleft; struct coord bottomrt; } mybox={{1.8,8.3},{12.4,1.29} };

  33. 结构变量的初始化 形式三: struct { 类型标识符 成员名; 类型标识符 成员名; ……………. }结构类型变量={初始数据};

  34. 结构变量的初始化 例 struct { int num; char name[20]; char sex; int age; char addr[30]; }stu1={112,“Wang Lin”,‘M’,19, “200 Beijing Road”};

  35. 结构变量的初始化 例 struct { char name[20]; long order; int score[5]; float average; }who={“Wang Lin”,031112,{92,91,89,87,94},0.0}; 例 struct coord { float x,y;}; struct { struct coord topleft; struct coord bottomrt; } mybox={{1.8,8.3},{12.4,1.29}};

  36. 结构类型定义与引用 结构类型和指针 结构数组 链表 联合类型 本讲重点

  37. 结构类型和指针 指向结构类型变量的指针 • 定义形式:struct 结构类型名 *结构类型指针名; 例 struct student *p; 存放结构类型变量在内存的起始地址 struct student stu1; struct student *p=&stu1; stu1.num=101;  (*p).num=101

  38. p num name struct student { int num; char name[20]; char sex; int age; }stu; struct student *p=&stu; stu sex age 结构类型和指针

  39. 结构类型和指针 • 使用结构类型指针变量引用成员形式 (*结构类型指针名).成员名 结构类型指针名->成员名 结构类型变量名.成员名 指向运算符 优先级: 1 结合方向:从左向右 例 int n; int *p=&n; *p=10; n=10

  40. 例8.3 指向结构类型的指针变量 int main(void) { struct student { long int num; char name[20]; char sex; float score; }stu_1,*p; p=&stu_1; stu_1.num=89101; strcpy(stu_1.name,"Li Lin"); p->sex='M'; p->score=89.5; printf("\nNo:%ld\nname:%s\nsex:%c\nscore:%f\n", (*p).num,p->name,stu_1.sex,p->score); return 0; }

  41. p num name stu[0] sex age p+1 stu[1] stu[2] 例 指向结构类型数组的指针 struct student { int num; char name[20]; char sex; int age; }stu[3]={{10101,"Li Lin",'M',18}, {10102,"Zhang Fun",'M',19}, {10104,"Wang Min",'F',20}}; int main(void) { struct student *p; for(p=stu;p<stu+3;p++) printf("%d%s%c%d\n",p->num,p->name,p->sex,p->age); return 0; }

  42. p a[0] p a[1] 结构类型和指针 /*L8-3.C: 分析下面程序的运行结果*/ #include <stdio.h> int main(void) { struct { int x; int y; }a[2]={{1,2},{3,4}},*p=a; printf(“%d,”,++p->x); printf(%d\n”,(++p)->x); return 0; } 2 1 2 3 4 2,3

  43. 结构类型定义与引用 结构类型和指针 结构数组 链表 联合类型 本讲重点

  44. num num name name 25B stu[0] sex sex age age stu[1] 结构数组的定义 三种形式: 形式一: struct student { int num; char name[20]; char sex; int age; }; struct student stu[2];

  45. num num name name 25B stu[0] sex sex age age stu[1] 结构数组的定义 形式二: struct student { int num; char name[20]; char sex; int age; }stu[2];

  46. num num name name 25B stu[0] sex sex age age stu[1] 结构数组的定义 形式三: struct { int num; char name[20]; char sex; int age; }stu[2];

  47. 分行初始化: struct student { int num; char name[20]; char sex; int age; }; struct student stu[ ]={{100,“Wang Lin”,‘M’,20}, {101,“Li Gang”,‘M’,19}, {110,“Liu Yan”,‘F’,19}}; 全部初始化时维数可省 结构数组初始化

  48. 结构数组初始化 顺序初始化: struct student { int num; char name[20]; char sex; int age; }; struct student stu[ ]={100,“Wang Lin”,‘M’,20, 101,“Li Gang”,‘M’,19, 110,“Liu Yan”,‘F’,19};

  49. 结构数组初始化 例 struct { int num; char name[20]; char sex; int age; }stu[ ]={{……},{……},{……}}; 例 struct student { int num; char name[20]; char sex; int age; }stu[ ]={{……},{……},{……}};

  50. stu[1].age++; struct student { int num; char name[20]; char sex; int age; }str[3]; strcpy(stu[0].name,”ZhaoDa”); 结构数组引用 • 引用方式: 结构数组名[下标].成员名

More Related