赞
踩
qsort进行排序的数组存储的不能是结构体的指针,需要是结构体本身。
struct student{
char* id;
int mark;
}arr[4],
test0={"0001",80},
test1={"0002",90},
test2={"0003",60},
test3={"0004",61}
;
int cmp(const void *a, const void *b){
int mark1=((struct student *)a)->mark;
int mark2=((struct student *)b)->mark;
return mark1>mark2 ? 1:-1;
}
#include <stdio.h> struct student{ char* id; int mark; }arr[4], test0={"0001",80}, test1={"0002",90}, test2={"0003",60}, test3={"0004",61} ; int cmp(const void *a, const void *b){ int mark1=((struct student *)a)->mark; int mark2=((struct student *)b)->mark; return mark1>mark2 ? 1:-1; } int main(){ arr[0]=test0;arr[1]=test1;arr[2]=test2;arr[3]=test3; printf("—————排序前—————\n"); for(int i=0; i<4; i++) printf("%s %d\n",arr[i].id,arr[i].mark); qsort(arr,4,sizeof(struct student),cmp); printf("—————排序后—————\n"); for(int i=0; i<4; i++) printf("%s %d\n",arr[i].id,arr[i].mark); return 0; }
—————排序前—————
0001 80
0002 90
0003 60
0004 61
—————排序后—————
0003 60
0004 61
0001 80
0002 90
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。