例11.5有一个结构体变量stu,内含学生学号、姓名和3门课的成绩。要求在main函数中赋以值,在另一函数print中将它们打印输出。今用结构体变量作函数参数。
#include <string.h>
#define FORMAT "%d\n%s\n%f\n%f\n%f\n"
struct student
{int num;
char name[20];
float score[3];
};
main()
{void print(struct student);
struct student stu;
stu.num=12345;
strcpy(stu.name,"Li Li");
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.6;
print(stu);
}
void print(struct student stu)
{printf(FORMAT,stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2]);
printf("\n");
}
运行结果为:
12345
Li Li
67.500000
89.000000
78.599998
#include <string.h>
#define FORMAT "%d\n%s\n%f\n%f\n%f\n"
struct student
{int num;
char name[20];
float score[3];
};
main()
{void print(struct student);
struct student stu;
stu.num=12345;
strcpy(stu.name,"Li Li");
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.6;
print(stu);
}
void print(struct student stu)
{printf(FORMAT,stu.num,stu.name,stu.score[0],stu.score[1],stu.score[2]);
printf("\n");
}
运行结果为:
12345
Li Li
67.500000
89.000000
78.599998