使用malloccalloc和free函数出错
Biny 2013-03-12 09:42:50 //定义结构体数组
#include <stdio.h>
#include <string.h>
struct struct name
{
char name[8]; //存放姓名
int number; //存放学号
int clas; //存放班级
float score; //存放成绩
};
struct struct name student[5],temp;
void sort(struct struct name student[])
{
struct struct name temp;
int i,j;
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
if(student[i].score<student[j].score)
{
temp=student[i];
student[i]=student[j];
student[j]=temp;
}
}
void main()
{
int i,j;
char name1[8];
int xh,nj;
float x;
//输入每名学生的信息
for(j=0;j<5;j++)
{
printf("\n input name ,number,class,score:");
scanf("%s%d%d%f",name1,&xh,&nj,&x);
strcpy(student[j].name,name1);
student[j].number=xh;
student[j].clas=nj;
student[j].score=x;
}
sort(student);
for(i=0;i<5;i++)
printf("\n name:%6s number:%d class:%d
score:%6.1f",student[i].name,student[i].number,student[i].clas,student[i].score);
}