Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int compare1 (const void *a, const void *b)
{
const int *x = a; /* 'x' points to a read-only integer given by 'a' */
const int *y = b; /* 'y' points to a read-only integer given by 'b' */
if ( *x < *y )
{
/* the int pointed to by 'x' is less than the int pointed to by 'y' */
return -1;
}
if ( *x > *y )
{
/* the int pointed to by 'x' is greater than the int pointed to by 'y' */
return 1;
}
return 0;
}
int compare2 (const void *a, const void *b)
{
return *(int*)a - *(int*)b;
}
void print (int *array, size_t size)
{
while ( size-- )
{
printf ("%d ", *array++);
}
putchar('\n');
}
int main ()
{
int values[] = { INT_MIN + 1, 0, 1, INT_MAX - 1 };
qsort ( values, sizeof values / sizeof *values, sizeof *values, compare1);
print ( values, sizeof values / sizeof *values);
qsort ( values, sizeof values / sizeof *values, sizeof *values, compare2);
print ( values, sizeof values / sizeof *values);
return 0;
}
/* my output
-2147483647 0 1 2147483646
0 1 2147483646 -2147483647
*/