A more "generic" approach:
Code:
#include <stdlib.h>
// Comparison funcions named after type and identifier.
#define QSORT_CMP_FN_ASC(T,FN) FN ## _ ## T ## _A
#define QSORT_CMP_FN_DESC(T,FN) FN ## _ ## T ## _D
// Definition of comparison functions based on type and identifier.
#define QSORT_CMP_ASC(T,FN) \
int QSORT_CMP_FN_ASC(T,FN) ( const void *a, const void *b ) \
{ \
return (*(const T *)a > *(const T *)b) - \
(*(const T *)a < *(const T *)b); \
}
#define QSORT_CMP_DESC(T,FN) \
int QSORT_CMP_FN_DESC(T,FN) ( const void *a, const void *b ) \
{ \
return (*(const T *)a < *(const T *)b) - \
(*(const T *)a > *(const T *)b); \
}
// qsort() function call based on ponter to buffer, number of elements, type and identifier
#define QSORT_ASC_CALL(ptr,E,T,FN) \
qsort( ptr, E, sizeof(T), QSORT_CMP_FN_ASC(T,FN) )
#define QSORT_DESC_CALL(ptr,E,T,FN) \
qsort( ptr, E, sizeof(T), QSORT_CMP_FN_DESC(T,FN) )
// Define ascending comparison static function for ints (qsort).
static QSORT_CMP_ASC(int, cmp)
// Example for ascending sort.
void sort( int *p, size_t elements )
{ QSORT_ASC_CALL( p, elements, int, cmp ); }