#include<stdio.h> #define N 3 struct student { long int num; char name[20]; float score[3]; float aver; }; int main() { void Input(struct student stu[]); struct student max(struct student stu[]); void print(struct student stud); struct student stu[N],*p=stu; Input(p); print(max(p)); return 0; } void Input(struct student stu[]) { int i; for(i=0;i<N;i++) { printf("输入第 %d 个学生的信息\n依次输入学号,姓名,三门课的成绩\n",i+1); scanf("%ld%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0; } } struct student max(struct student stu[]) { int i,m=0; for(i=1;i<N;i++) if(stu[i].aver>stu[m].aver) m=i; return stu[m]; } void print(struct student stud) { printf("成绩最高学生的信息\n"); printf("%ld,%s,%.2f,%.2f,%.2f,%.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver); }
今晚写了一个结构体指针的代码,出现了好多问题,经过同学的一步一步分析,终于出结果了。出现的第一个问题是scanf(“%ld,%d\n”),一旦加了\n,将会直接导致输入出现问题
在一个问题就是char name[20],在输入时,不能加&(取地址符)
c语言结构体数组指针
最新推荐文章于 2024-06-05 11:24:56 发布