I want to use quicksort in C, which has a function signature of void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
, but the signature of my comparison function is int (*compar)(const void *, const void*, const int)
with the third argument being constant during one call to quicksort.
As an illustration, assume that I want to sort an array of vectors according to different norms (L0, L1, L2 and Linifinity norm for example). Which norm it actually is, is passed as a third argument to the comparison function, but remains constant during the call of qsort
. Is it possible to do an assignment in a form like
//Function declaration for parametric comparison
int cmp3(int* a_vec, int* b_vec, int x);
// Somewhere in main
int (*cmp2)(int, int);
cmp2 = cmp3(int*, int*, 2);//2 could mean L2 norm
to be able to call something like
qsort(a, 100, sizeof(a), cmp2);
I know this does not work, but I hope it gives an idea what I want to accomplish. Also it is not possible to make different comparison functions and calls to qsort as the number of different ways of comparing is too big.