整型
#include<stdlib.h>
int cmp(const void *a,const void *b )
{
return *(int*)a-*(int*)b;//从小到大//*(int*)b-*(int*)a从大到小
}
字符型
int cmp(const void *a,const void *b)
{
return *(char*)a-*(char*)b;
}
{
return *(char*)a-*(char*)b;
}
浮点型
int cmp(const void *a,const void *b)
{
return *(float*)a>*(float*)b?1:-1;
}
{
return *(float*)a>*(float*)b?1:-1;
}
int cmp(const void *a,const void *b)
{
return *(double*)a>*(double*)b?1:-1;
}
{
return *(double*)a>*(double*)b?1:-1;
}
*(float*)a-*(float*)b;这种比较可能会返回0,编译器判断为相等
结构体
int cmp(const void *a,const void *b)
{
return (*(struct th* )a).x-(*(struct thi*)b).x;//注意struct th 中的(struct)不能少。
}
{
return (*(struct th* )a).x-(*(struct thi*)b).x;//注意struct th 中的(struct)不能少。
}
((struct shabi* )a)->x==((struct shabi*)b)->x)
strcmp( (*(struct In *)a).str , (*(struct In *)b).str )