1. 程式人生 > >c語言結構體陣列指標

c語言結構體陣列指標

#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],在輸入時,不能加&(取地址符)