You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to qsort() an array with a comparison operator that takes parameters
you need to use global variables to pass those parameters (not possible when
writing multithreaded code), or use qsort_r/qsort_s which are not portable
(there are separate GNU/BSD/Windows versions and they all take different arguments).
So I wrote a portable qsort_r/qsort_s called sort_r():
base is the array to be sorted
nel is the number of elements in the array
width is the size in bytes of each element of the array
compar is the comparison function
arg is a pointer to be passed to the comparison function
Using sort_r
Add #include "sort_r.h" to the top of your code. Then copy sort_r.h into your
working directory, or add -I path/to/sort_r to your compile arguments.
Build Example
Compile example code (example.c) with:
make
To build using nested functions and qsort instead of qsort_r use
make NESTED_QSORT=1
Nested functions are not permitted under ISO C, they are a GCC extension.
License
Public Domain. Use as you wish. No warranty. There may be bugs.