主函数 #include <stdio.h> define NUMOFCOURSE 5 *define N 30 struct student [int num:/学号 char name20]:姓名*/ char sex;/性别:m-男,f-女 float score[ NUMOFCOURSE]:成绩*/ float aver;/平均分 :/*定义结构体类型 struct studen,一般都定义为全局可用的数据类型*/ typedef struct student STU/为数据类型起别名 void Input( STU aD, int n) void maino STU stu[N];定义结构体变量*/ Input(stu, N) printf"num\tname\t sex c1 c2 c3 c4 c5 aver \n) for(i=0:i<N;i++)/输出学生基本信息*/ printf("%dt%sⅥ‰“,.s九u[]mum,su[].name,sthu[i]sex) forgj=0; j< NUMOFCOURSE; j++) printf(93Of", stu[i]. scoreLj] printf(93Of, stu[i]. average)
11 主函数 #include <stdio.h> #define NUMOFCOURSE 5 #define N 30 struct student {int num; /* 学号 */ char name[20]; /* 姓名 */ char sex; /* 性别:m-男,f-女 */ float score[NUMOFCOURSE]; /* 成绩 */ float aver; /* 平均分 */ }; /* 定义结构体类型 struct student ,一般都定义为全局可用的数据类型*/ typedef struct student STU;/*为数据类型起别名*/ void Input( STU a[],int n); void main() { int i,j; STU stu[N];/* 定义结构体变量 */ Input(stu,N); printf("num\tname\t sex c1 c2 c3 c4 c5 aver\n"); for(i=0;i<N;i++) /* 输出学生基本信息 */ { printf("%d\t%s\t %c “,stu[i].num, stu[i].name, stu[i].sex); for(j=0;j< NUMOFCOURSE;j++) printf("%3.0f ", stu[i].score[j]; printf("%3.0f ", stu[i].average); } }
对数组元素的成员进行用 1如果有指针变量p指向数组stu, stuli1如何定义指针,如何用指针引 用数组元素的成员呢? sTu 3.P=? stu[o] 1d 4.?num 88.8 s1) 5.P++以后到哪儿? 87.4 sTu 10五 89 84.8 su3]104何六M89 63 76 77 stu[4] 105周七 82 106 struc↑ student*p p=stu °.p->mum或(p)mum女su[imum 2
12 对数组元素的成员进行引用 有三种方式 ▪ stu[i].num 101 张三 M 80 90 98 86 90 88.8 102 李四 M 82 89 94 81 91 87.4 103 王五 F 92 78 86 79 89 84.8 104 何六 M 89 81 78 63 76 77.4 105 周七 F 98 69 91 85 67 82 106 … … stu stu[0] stu[1] stu[2] stu[3] stu[4] …… 1.如果有指针变量p指向数组stu, 如何定义指针,如何用指针引 用数组元素的成员呢? 2.? *p; 3.P=?; 4.?num 5.P++以后到哪儿? •struct student *p; •p=stu; •…p->num或(*p).num ➔ stu[i].num