OK, here's some complete code. There's a different in the compare function to the one I posted earlier, both of it's parameters are declared as const void* now.
Code:
#include <stdio.h>
#include <stdlib.h>
void printarray(int a[], int size)
{
int i;
for (i = 0; i < size; i++)
printf ("%d\n", a[i]);
}
int compare(const void *p1, const void *p2)
{
int *key1 = (int *) p1;
int *key2 = (int *) p2;
return (*key1 - *key2);
}
int main(void)
{
int myarray[] = {5, 4, 1, 9, 5, 10};
int num_elements = sizeof(myarray)/sizeof(myarray[0]);
puts("Before");
printarray(myarray, num_elements);
qsort (myarray, num_elements, sizeof(int), compare);
puts("After");
printarray(myarray, num_elements);
return 0;
}
/* Program output
Before
5
4
1
9
5
10
After
1
4
5
5
9
10
*/