一个有关指向结构体数组的指针的问题
如下程序功能是:从主函数中输入5个学生的数据记录,从print函数输出这些纪录。 #include<stdio.h>struct student
{
int num;
char name[20];
float score[3];
}stu[5];
main()
{
void print(struct student *);
int i;
printf("input 5 students' information:\n");
for(i=0;i<=4;i++)
scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
print(stu);
}
void print(struct student *p)
{
int i;
printf("No. name score1 score2 score3\n");
for(i=0;i<=4;i++,p++)
printf("%4d %-20s %f %f %f\n",(*p).num,(*p).name,(*p).score[0],(*p).score[1],(*p).score[2]);
} 编译连接都没有错,但是结果如下,不太正确
input 5 students' information:
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87
12312,hanfang,78.5,98.5,87 /*我懒省劲,重复输入12312,hanfang,78.5,98.5,87五次*/
No. name score1 score2 score3
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
12312 hanfang,78.5,98.5,87 0.000000 0.000000 0.000000
Press any key to continue