#include<stdio.h> #include<malloc.h> #include<string.h> //动态构造学生管理系统 struct Student { int age; float score; char name[100]; }; int main(void) { int len; struct Student * pArr; int i,j; struct Student t; //动态的构造一维数组 printf("请输入学生个数: "); printf("len="); scanf("%d",&len); pArr = (struct Student *)malloc(len*sizeof(struct Student)); //输入 for(i=0;i<len;i++) { printf("请输入第%d个学生信息: ",i+1); printf("age="); //scanf("%d",&(*(pArr+i).age)); scanf("%d",&(pArr[i].age)); printf("score="); scanf("%f",&(pArr[i].score)); printf("name="); scanf("%s",pArr[i].name);//name是数组名,本身是数组首元素地址,所以不需要加& } //冒泡排序 for(i=0;i<len-1;i++) { for(j=0;j<len-1-i;j++) { if(pArr[i].score>pArr[i+1].score) { t=pArr[i]; pArr[i]=pArr[i+1]; pArr[i+1]=t; } } } //输出 for(i=0;i<len;i++) { printf("第%d个学生信息: ",i+1); printf("age=%d ",pArr[i].age); printf("score=%f ",pArr[i].score); printf("name=%s ",pArr[i].name); } return 0; }