第十一章结构体与共用体 111结构体的定义与引用 1定义结构体 定义有n个成员的结构体类型的一般形式 struct结构体类型标识符 类型标识符1成员名1; 类型标识符2成员名2; 类型标识符n 成员名n; 例1: 要求定义关于学生通讯录的结构体类型 struct namel,其中包括 以下数据:姓名(长度为10的字符数组)、性别(字符型)、地址(长 度为20的数组)、电话(整型) struct student name[10] char addr[20; int tel 2.定义结构体类型变量 (1)定义变量的一般形式: step l:定义好一个结构体类型后 step2:定义变量 struct结构体类型标识符变量名列表 注:关键字 struct与结构体类型标识符一起使用。 Struct struct namel stul stu2 (2)直接定义结构体变量 struct student name[10] char addr[20; 11.2结构体变量的使用 1.成员的引用形式为 结构体变量名.成员名∥.称成员运算符 例1:定义两个变量 struct student stul,stu2;
第十一章 结构体与共用体 11.1 结构体的定义与引用 1.定义结构体 定义有 n 个成员的结构体类型的一般形式: struct 结构体类型标识符 { 类型标识符 1 成员名 1; 类型标识符 2 成员名 2; ….. ……. 类型标识符 n 成员名 n; } 例 1: 要求定义关于学生通讯录的结构体类型 struct_name1,其中包括 以下数据:姓名(长度为 10 的字符数组)、性别(字符型)、地址(长 度为 20 的数组)、电话(整型) struct student { char name[10]; char sex; char addr[20]; int tel; }; 2. 定义结构体类型变量 ⑴ 定义变量的一般形式: step1: 定义好一个结构体类型后 step2: 定义变量 struct 结构体类型标识符 变量名列表; 注:关键字 struct 与结构体类型标识符一起使用。 Struct struct_name1 stu1,stu2; ⑵直接定义结构体变量: struct student { char name[10]; char sex; char addr[20]; } stu1,stu2; 11.2 结构体变量的使用 1. 成员的引用形式为: 结构体变量名.成员名 // . 称成员运算符 例 1: 定义两个变量 struct student stu1,stu2;
引用 strcpy( surname,李红”) strcpy( stuladdr,湛江师范学院”) 思考:是否可以写成 studentlname=“宋红” ?如何输出 2结构体变量的初始化 除了可以像上述那样为结构变量的成员一一赋值外,还可以在结 构变量定义时对其进行初始化. struct date int month Int year stul={10,22,89} 又如 struct datestu2=(1, 25, 88 两个相类型的结构体变量可以互相赋值:stu2=stul: 3结构的嵌套 结构的成员可以是另一个结构引用内层结构的成员时要包含 两个结构变量的名字 int month Int year struct student char name[1o char addr[20] date birthday struct student studentI={“李红”,M’,'湛江师范学院”,{9,23,80}; printf(" birthday is %od-%od-%d, StudentI birthday month, studentI birthday day, studentI birthday year)
引用 strcpy(stu1.name,”李 红”); stu1.sex=’M’; strcpy(stu1.addr,”湛江师范学院”); 思考:是否可以写成 student1.name=“宋 红”; ? 如何输出 2.结构体变量的初始化 除了可以像上述那样为结构变量的成员一一赋值外,还可以在结 构变量定义时对其进行初始化. struct date { int month; int day; int year; } stu1={10, 22, 89}; 又如: struct date stu2={1,25,88}; 两个相类型的结构体变量可以互相赋值: stu2=stu1; 3.结构的嵌套 结构的成员可以是另一个结构.引用内层结构的成员时要包含 两个结构变量的名字. struct date { int month; int day; int year; } struct student { char name[10]; char sex; char addr[20]; date birthday; } struct student student1={“李 红”,’M’,”湛江师范学院”,{9,23,80}}; printf(“birthday is %d-%d-%d”,student1.birthday.month, student1.birthday.day,student1.birthday.year)
11.3结构体数组 数组的每个元素是结构体类型 Struct date birthday[3={{1,3,1977},{123,199},{1ll2,1980}}; Birthday[o) month=5 Birthday[o]. day=24 Birthday[o] year1988; 例2设李红、王建和赵明三个同学某学年考了8门课,现在要求 分别统计出这三名同学该学年的总成绩,并按8门课总成绩的高低排 序输出。 struct name /*定义结构体类型* char namel8];/姓名* float score, /*8门课总成绩* }; maino Int 1, 1; float x. struct nameemp, stul li hong, 01, wang,0), i zhao, 0) for(i=1;i<=8;i++) printf("n输入第%d门课的成绩:n”,i); for(=0j<3j++) printi(“姓名%s成绩为, std[i] name) scanf( %of, &x) std[]. score=std [].score+x for(i=0;i<2;i++) for(j=计+1;j3;j++) if(std]. score<std1]. score i temp=std std[l=std] stdiN=temp *输出结果* for(i=0;i<3;i++) printf((n姓名%总成绩:%6 stdi]. name, std]. score)
11.3 结构体数组 数组的每个元素是结构体类型. Struct date birthday[3]={{1,3,1977},{12,23,199},{11,12,1980}}; Birthday[0].month=5; Birthday[0].day=24; Birthday[0].year=1988; 例 2.设李红、王建和赵明三个同学某学年考了 8 门课,现在要求 分别统计出这三名同学该学年的总成绩,并按 8 门课总成绩的高低排 序输出。 struct name /* 定义结构体类型 */ { char name[8]; /* 姓名 */ float score; /* 8 门课总成绩 */ }; main() { int i, j; float x; struct name emp, stu[]={{“li hong”,0},{“wang”,0},{“zhao”,0}}; for (i=1; i<=8;i++) { printf(“\n 输入第%d 门课的成绩:\n”, i); for (j=0;j<3;j++) { printf(“姓名:%s 成绩为:”, std[j].name); scanf(“%f”, &x); std[j].score = std[j].score +x; } } for ( i=0; i<2; i++) for ( j=i+1; j<3; j++) if (std[i].score<std[j].score) { temp=std[j]; std[j]=std[i]; std[i]=temp; } /* 输出结果 */ for ( i=0; i<3; i++) printf( “\n 姓名:%s 总成绩:%6.1f”, std[i].name,std[i].score); }
114指向结构体变量的指针 1定义 指向结构体指针的定义,与结构体变量的定义完全类似 truct date int month Int year, struct date p, birthday 2引用 指针引用结构体中的成员变量 (*p)成员名 或p→成员名更直观 struct date p, birthday=(4, 22, 199 p=&birthday; p->month=6 p-day p->year=1988; 3.指向结构体数组的指针 struct date*p, birthday[3}={422,199},{6,23,1978},{9,23,1999} p=birthday 思考:赋值语句 p=st+1;和p++; 代表指针p指向哪里? 结构体数组 birthday 22 birthday[o] 23 birthday] 1978 birthday 2] l99
11.4 指向结构体变量的指针 1.定义: 指向结构体指针的定义, 与结构体变量的定义完全类似。 struct date { int month; int day; int year; } struct date *p, birthday; 2.引用: 指针引用结构体中的成员变量: (*p).成员名 或 p->成员名 //更直观 struct date *p, birthday={4,22,199}; p=&birthday; p->month=6; p->day=9; p->year=1988; 3. 指向结构体数组的指针 struct date *p, birthday[3]={{4,22,1999},{6,23,1978},{9,23,1999}}; p=birthday; 思考:赋值语句 p=std+1;和 p++; 代表指针 p 指向哪里?
11.5结构与函数 结体变量可以象其他类型的变量一样作为参数传递给其它函数,或 者从被调函数中将函数值返回调用处。 例3.有一个结构体变量stu,内含学生学号、姓名和3门成绩,要 求在main函数中赋值,在另一 printscore函数中将期打印出来 Include <string. #define format %od\ n%sn%fn%fn%fn Struct student iint char name [ 20] float score3 Maino void printscore(struct student) struct stu Stu num=1234: strcpy(stu. name, Lili) Stu. score0675 Stu Score[1]=89 Stuscore21=78.9; printscore(stu); Void printscore(struct student stu) printf(format, stu num, stu. name, stu score[OJ, stu score[O], stu score[OD; 116链表 1链表定义 所谓链表,是指一种动态的进行存储分配的一种数据结构
11.5 结构与函数 结体变量可以象其他类型的变量一样作为参数传递给其它函数,或 者从被调函数中将函数值返回调用处。 例 3.有一个结构体变量 stu,内含学生学号、姓名和 3 门成绩,要 求在 main 函数中赋值,在另一 printscore 函数中将期打印出来 #include <string.h> #define Format “%d\n%s\n%f\n%f\n%f\n” Struct student { int num; char name[20]; float score[3]; }; Main() { void printscore(struct student); struct student stu; Stu.num=1234; strcpy(stu.name,”Li li”); Stu.score[0]=67.5; Stu.score[1]=89; Stu.score[2]=78.9; printscore(stu); } Void printscore(struct student stu) { printf(format,stu.num,stu.name,stu.score[0], stu.score[0], stu.score[0]); } 11.6 链表 1.链表定义 所谓链表,是指一种动态的进行存储分配的一种数据结构