关于结构体的简单问题

来源:百度知道 编辑:UC知道 时间:2024/06/01 10:49:39
#include<stdio.h>
struct student
{
long num;
char name[20];
float score[3];
};
void main()
{
void print(struct student);
struct student stu[5];
stu[5]={{1,"Li",56,78,34},
{2,"Zhang",79,58,84},
{3,"Luo",76,48,94},
{4,"Cai",55,79,68},
{5,"He",60,80,84}};
print(stu);
}

void print(struct student stu)
{
int i;
for(i=0;i<5;i++)
printf("%d\n%s\n%d\n%d\n%d\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
printf("\n");
}
才学啊 ,不知道哪里错了 请帮忙指正一下,附上简单讲述,谢谢啦

1、给结构体赋值,要求在定义的时候初始化。
2、参数传递要求类型匹配。你的stu是结构体数组,传递给print时候,也要是。
3、主要score是float,输出的时候不是%d,是%f。
#include<stdio.h>
struct student
{
long num;
char name[20];
float score[3];
};
void main()
{
void print(struct student[]); //传参数要求是数组。
struct student stu[5]={{1,"Li",56,78,34},
{2,"Zhang",79,58,84},
{3,"Luo",76,48,94},
{4,"Cai",55,79,68},
{5,"He",60,80,84}}; //此处定义初始化
print(stu);
}

void print(struct student stu[])
{
int i;
for(i=0;i<5;i++)
printf("%d\n%s\n%f\n%f\n%f\n",stu [i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]); //改正为%f
printf("\n");
}

程序修正了,你可以验证一下。
希望对你有帮助。

//---------------------------------------------------------------------------

#include<stdio.h>
stru