sample code
#include <stdio.h>
#include <stdlib.h>
int compare ( const void *pa, const void *pb ) {
const int *a = *(const int **)pa;
const int *b = *(const int **)pb;
if(a[0] == b[0])
return a[1] - b[1];
else
return a[0] - b[0];
}
/*
#define NUMCMP(x,y) (((x) < (y)) ? -1 : ((x) > (y)) ? 1 : 0)
int compare ( const void *pa, const void *pb ) {
const int (*a)[2] = *(const int (**)[2])pa;
const int (*b)[2] = *(const int (**)[2])pb;
int tmp;
if((tmp=NUMCMP((*a)[0], (*b)[0]))==0)
return NUMCMP((*a)[1], (*b)[1]);
else
return tmp;
}
*/
int main(void){
int **array;
int number = 10;
int i;
array = malloc(number * sizeof(int*));
for (i = 0; i < number; i++){
array[i] = malloc(2 * sizeof(int));
array[i][0] = rand()%20;
array[i][1] = rand()%20;
}
for(i = 0;i < number;++i)
printf("%2d, %2d\n", array[i][0], array[i][1]);
printf("\n");
qsort(array, number, sizeof array[0], compare);
for(i = 0;i < number;++i)
printf("%2d, %2d\n", array[i][0], array[i][1]);
return 0;
}
what *(const int **)pa
array = {(int *), (int *), (int *), ... , (int *) }
qsort need each element address for element (for swap, etc. Because the size and number and start address of the element since only the given information).
E.g &(int *), &(int *)
so (int **)
pass to function compare.
call compare(int **, int **)
&(int*)
meant at arg int**
compare function prototypeis cmp(const void*, const void*)
cast (const int**)pa
is cast to passed original pointer.
*((const int **)pa)
is dereference original element pointer(int*
)