请问以下示例有什么问题?我从这里获取了它,并用
unsigned long
替换了int
。我还更改了cmpfunc
以正确处理unsigned long
。#include <stdio.h>
#include <stdlib.h>
unsigned long values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void * a, const void * b)
{
if(*(unsigned long*)a - *(unsigned long*)b < 0){
return -1;
}
if(*(unsigned long*)a - *(unsigned long*)b > 0){
return 1;
}
if(*(unsigned long*)a - *(unsigned long*)b == 0){
return 0;
}
}
int main()
{
int n;
printf("Before sorting the list is: \n");
for( n = 0 ; n < 5; n++ )
{
printf("%lu ", values[n]);
}
qsort(values, 5, sizeof(unsigned long), cmpfunc);
printf("\nAfter sorting the list is: \n");
for( n = 0 ; n < 5; n++ )
{
printf("%lu ", values[n]);
}
return(0);
}
这是我得到的输出:
Before sorting the list is:
88 56 100 2 25
After sorting the list is:
25 2 100 56 88