This is a qsort sample function i found on google.
Code:
/* qsort example */
#include <stdio.h>
#include <stdlib.h>
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int * pItem;
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++)
{
printf ("%d ",values[n]);
}
return 0;
}
But I couldn't understand the function compare.
Why void? Why *(int*)a ?
Actually I'm coding a program and I wouldn't use pointers yet until I understand it. But I presume that is impossible to use qsort function without using pointers. I don't like to import lines to my code without know as it works. "Copy and paste" is a lammer/newbie way. Please, help me to understand this function.