If you use Glibc (i.e. Linux), you can use qsort_r :
int compareme (const void *a, const void *b, void *data) { float c = *(float *)data; float tmp = f((float*)a, (float*)b, c); if (tmp < 0) return -1; if (tmp == 0) return 0; if (tmp > 0) return 1; }
then call
qsort_r(float *arr, n, sizeof(float), compareme, &c);
You must define the _GNU_SOURCE preprocessor macro before including any headers to get this (for example, using -D_GNU_SOURCE ) to get this function, and this will limit your portability of the program.
Otherwise, you will have to use global or streaming storage or write your own sort function.
source share