void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(constvoid *, constvoid *));
#include <stdlib.h>
int int_sorter( constvoid *first_arg, constvoid *second_arg )
{
int first = *(int*)first_arg;
int second = *(int*)second_arg;
if ( first < second )
{
return -1;
}
elseif ( first == second )
{
return 0;
}
else
{
return 1;
}
}
int main()
{
int array[10];
int i;
/* fill array */
for ( i = 0; i < 10; ++i )
{
array[ i ] = 10 - i;
}
qsort( array, 10 , sizeof( int ), int_sorter );
for ( i = 0; i < 10; ++i )
{
printf ( "%d\n" ,array[ i ] );
}
}
casting first_arg to an integer pointer, and then dereferencing that pointer to get a value (i.e. int first).
void pointers can be used to pass around different stuff: http://www.learncpp.com/cpp-tutorial/613-void-pointers/
The first_arg is a pointer to constvoid object. The (int*) is a cast that the object is actually an int, i.e. the first_arg is temporarily a pointer to int.
Dereferencing a pointer returns the pointed to value.