1 / 30

10.3 结构数组

10.3 结构数组. 一个结构体变量可以存放一组数据(如一个 学生的学号、姓名、成绩等数据)。如果需要有 10 个学生的数据需要参加运算,就要使用数组, 这就是结构体数组。. 结构数组的每一个元素,都是结构类型数据,均包含结构类型的所有成员。. 一、结构体数组的定义 定义结构体数组和定义结构体变量的方法相仿。. 例 3 : 利用例 1 中定义的结构类型 struct student 定义一个结构数组 stu ,用于存储和显示三个学生的基本情况。. struct date { int year; int month; int day;};

lavada
Download Presentation

10.3 结构数组

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. 10.3 结构数组 一个结构体变量可以存放一组数据(如一个 学生的学号、姓名、成绩等数据)。如果需要有 10个学生的数据需要参加运算,就要使用数组, 这就是结构体数组。 结构数组的每一个元素,都是结构类型数据,均包含结构类型的所有成员。

  2. 一、结构体数组的定义 定义结构体数组和定义结构体变量的方法相仿。 例3: 利用例1中定义的结构类型struct student 定义一个结构数组stu,用于存储和显示三个学生的基本情况。

  3. struct date { int year; int month; int day;}; struct student { char no[7]; char name[9]; char sex[3]; struct date birthday; }; struct student stu ; [3]

  4. 二、结构体数组的初始化 struct student stu[3]={{“000102”,“zhangsan”,“m”,{1980,9,20}}, {“000103”,“lili”,“m”,{1980,8,15}}, {“000104”,“wangwu”,“f”,{1980,3,10}} };

  5. 练习一: 下列程序的运行结果是[ ]。 main() { struct st {int x; int y;}cnum[2]={1,3,2,7}; printf(“%d\n”,cnum[0].y/cnum[0].x*cnum[1].x); } 答案:6

  6. 练习二: 根据下面的定义,能打印出字母M的语句是[ ]。 struct person {char name[9];int age;} struct person class[10]={"John",17,"Paul",19,"May",18, "Adam",16}; A、printf("%c\n",class[3].name); B、printf("%c\n",class[3].name[1]); C、printf("%c\n",class[2].name[1]); D、printf("%c\n",class[2].name[0]); 答案:D

  7. 三、结构体数组应用举例 例10.2 对候选人得票的统计程序。设有10人投票, 3个候选人,每次输入一个候选人的名字,要求输 出各人的得票结果。

  8. #include"string.h" #define N 10 struct person { char name[20]; int count; }leader[3]={"li",0,"zhang",0,"feng",0}; main() { int i,j; char leader_name[20]; for(i=1;i<=N;i++) { scanf("%s",leader_name); for(j=0;j<3;j++) if(strcmp(leader_name,leader[j].name)==0) leader[j].count++; } printf("\n"); for(i=0;i<3;i++) printf("%5s:%-5d",leader[i].name,leader[i].count);}

  9. 10.4 指向结构体类型数据的指针 一、 指向结构体变量的指针 结构体变量在内存中的起始地址称为结构体变量的指针。

  10. 例.4 使用指向结构体变量的指针来访问结构体变量的各个成员。 struct date { int year; int month; int day; }; struct student {char no[7]; char name[9]; char sex[3]; struct date birthday;};

  11. (续) struct student stu={"000102","zhangsan","m", {1980,9,20}}; main( ) { struct student *p=&stu; printf("No: %s\n",(*p).no); printf("Name: %s\n",(*p).name); printf("Sex: %s\n",(*p).sex); printf("Birthday: %d-%d-%d\n",(*p).birthday.year, (*p).birthday.month, (*p).birthday.day); }

  12. 通过指向结构变量的指针来访问结构变量的成员有以下三种形式:通过指向结构变量的指针来访问结构变量的成员有以下三种形式: (1)var.成员 分量运算符左侧的运算对象,只能是结构体变量 (2 )(*pointer).成员 “*pointer”外面的括号不能省! (3 )为了使用方便和使之直观,把(*pointer).成员 用pointer- >成员代替。 注意:格式(2)(3)中,pointer只能是指向结构体变量(或结构数组)的指针变量,否则出错。

  13. 例如: struct student stu={"000102","zhangsan","m", {1980,9,20}}; main( ) {struct student *p=&stu; printf("No: %s\n",p->no); printf("Name: %s\n",p->name); printf("Sex: %s\n",p->sex); printf("Birthday: %d-%d-%d\n",p->birthday.year, p->birthday.month, p->birthday.day); }

  14. 练习一: 若有以下说明语句,则对pup中sex成员的正确引用是 struct population {char name[20]; int sex; }pup,*p; p=&pup; A) p.pup.sex; B) p->pup.sex; C) (*p).pup.sex; D) (*p).sex;

  15. 练习二: 以下对结构体变量stud1中成员age的非法引用是 struct student { int age; int num; }stu1,*p; p=&stu1; A) stu1.age; B) student.age; C) p->age; D)(*p).age;

  16. 思考题: 如果要求从键盘上输入结构变量student的各成员数据,如何修改程序?

  17. struct date { int year; int month; int day; }; struct student {char no[7]; char name[9]; char sex[3]; struct date birthday;}; struct student stu [3]={{"000102","zhangsan","m",{1980,9,20}}, {"00103","lili","m",{1980,8,15}}, {"00104","wangwu","f",{1980,3,10}} }; struct student *p=stu; 二、 指向结构体数组的指针 例.5 使用指向结构数组的指针来访问结构数组。

  18. (续) main() { for(;p<stu+3;p++) { printf("No: %s\t",p->no); printf("Name: %s\t",p->name); printf("Sex: %s\t",p->sex); printf("Birthday: %d-%d-%d\n",p->birthday.year, p->birthday.month, p->birthday.day); } }

  19. 注意: 1. 如果指针变量p已指向某结构数组,则p+1 指向结构数组的下一个元素,而不是当前元 素的下一个成员。 2. 如果指针变量p已经指向一个结构变量(或 结构数组),就不能再使之指向结构变量 (或结构数组元素)的某一成员。

  20. 练习三: 下列程序的运行结果是[ ]。 struct student {char name[20]; char sex; int age;}stu[3]={“Lilin”,’M’,18, “Yangfan”,’M’,21, “Liuchang”,’F’,19}; main() { struct student *p; p=stu; printf(“%s,%c,%d\n”,p->name,p->sex,p->age); }

  21. 若有以语句 struct student {int num; int age;} struct studetn stu[3]={(1001,20}, 1002,19}, {1003,21}}; main() {struct student *p; p=stu; ……} 则以下不正确的引用是[ ]。 A)(p++)->num B) p=&stu.age C) (*p).num D) p++ 练习四:

  22. 下列程序的运行结果是[ ]。 struct st {int x; int *y;}*p; int dt[4]={10,20,30,40}; struct st aa[4]={50,&dt[0],60,&dt[1],70,&dt[2],80,&dt[3]}; main() {p=aa; printf(“%d\n”,++p->x); printf(“%d\n”,(++p)->x); printf(“%d\n”,++(*p->y)); } 练习五: 答案:51,60,21

  23. struct s { int a; int b; }data[4]={10,100,20,200,30,300,40,400}; main() { struct s *pointer=data; printf("%d\n",++pointer->a); printf("%d\n",(++pointer)->b); printf("%d\n",pointer++->a); printf("%d\n",pointer->b++); printf("%d\n",pointer->b); } 练习六: 答案:11,200,20,300,301

  24. 三、结构体数据的指针作函数参数 (1)用结构体变量的成员作函数参数,用法与 普通变量作实参相同。 (2)用结构体变量作函数参数,采取“值传递” 的方式,将结构体变量所占内存单元的全 部内容按顺序传递给形参。 (3)用指向结构体变量(或数组)的指针作函数 参数,将结构体变量(或数组)的地址传给 形参。

  25. 例6、有一个结构体变量stu,内含学生学号、姓名和例6、有一个结构体变量stu,内含学生学号、姓名和 3门成绩。要求在main主函数中赋初值,在另一个 函数display中将它们打印出来。 #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]; };

  26. (续) main() { void display( struct student); struct student stu; stu.num=12345; strcpy(stu.name,”Lili”); stu.score[0]=67.5; stu.score[1]=89; stu.score[2]=78.6; display(stu); } void display(struct student stu) { printf(FORMAT,stu.num,stu.name,stu.score[0], stu.score[1],stu.score[2]); printf(“\n”); }

  27. 例7:用函数调用方式,改写例6编写一个专门的显示函数display(),通过主函数调用显示打印结果。例7:用函数调用方式,改写例6编写一个专门的显示函数display(),通过主函数调用显示打印结果。 #include"struct.h" /*定义并初始化一个外部结构数组student */ struct std_info student[3]={{"000102",“zhangsan",“M",{1980,5,20}}, {“000105”,“lisi”,“F”,{1980,8,15}}, {“000112”,“wangwu”,“M”,{1980,3,10}} };

  28. (续1) main() { void display(); /*函数说明*/ int i=0; for( ; i<3; i++) /*打印内容*/ { display( student + i ); printf("\n"); } }

  29. (续2) void display(struct student *p_std) { printf("%-7s%-9s%-4s", p_std->no, p_std->name, p_std->sex); printf("%4d-%2d-%2d\n", p_std->birthday.year, p_std->birthday.month, p_std->birthday.day); }

  30. 作业:习题二十一 实验:实验二十一

More Related