92结构数组 个结构变量只能存放一个学生的资料。 若班上有20个学生,需要用结构数组。 即,数组中的每个元素都是结构类型。 921定义 stulO struct student i 200011 Zhang85 stu][20012Li90 long int num char name[201 float score 3 stu20] stu19200090zhao|70
9.2 结构数组 一个结构变量只能存放一个学生的资料。 若班上有20个学生,需要用结构数组。 即,数组中的每个元素都是结构类型。 9.2.1 定义 struct student { long int num; char name[20]; float score; }stu[20]; stu[0] 200011 Zhang 85 stu[19] 200012 Li 90 200029 Zhao 70 stu[1]
stu[]「200011 Zhang85 stu[l][200012 Li 90 9,2,2初始化 stu19「200029Zhao|70 struct student i long int num; char name 201 float score, }stu[20={200011, Zhang:,85}, {200012,22,90}};
9.2.2 初始化 struct student { long int num; char name[20]; float score; }stu[20]={{200011,”Zhang”,85}, {200012,”Li”,90}}; stu[0] 200011 Zhang 85 stu[19] 200012 Li 90 200029 Zhao 70 stu[1]
923引用 stuo!□20001 Zhang85 stu[1200012Li90 struct student i long int num char name[20] float score, stu[19 200029 Zhao70 3[20 stu[O].num u name stu[O].score
9.2.3 引用 struct student { long int num; char name[20]; float score; }stu[20]; stu[0].num stu[0].name stu[0].score stu[0] 200011 Zhang 85 stu[19] 200012 Li 90 200029 Zhao 70 stu[1]
程序举例 例1、输入某班30位学生的姓名及数学、英语 成绩,计算并输出每位学生的平均分。 struct student char name 10] Int math, eng, float aver
程序举例 例1、输入某班30位学生的姓名及数学、英语 成绩,计算并输出每位学生的平均分。 struct student{ char name[10]; int math, eng; float aver; };
[0] Zhang 85 s[Li7790 void main( A struct student s 301; s[29[ wang 60 78 Int for(i=0;i<30;计++){ scanf(%os%d%od",si] name, &s]. math, &si. eng s].aver=(sl]. math+s1]. eng)/2.0 printf("%s%f,sli] name, si aver)
void main( ) { struct student s[30]; int i; for(i=0; i<30; i++) { scanf("%s%d%d", s[i].name, &s[i].math, &s[i].eng); s[i].aver = (s[i].math+s[i].eng)/2.0 printf("%s%f", s[i].name, s[i].aver); } } s[0] s[29] s[1] Zhang 80 85 Li 77 90 wang 60 78