qsorting & managing struct arrays, pointer arrays

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Oct 21 07:58:46 AEST 1989


In article <169 at cica.cica.indiana.edu> burleigh at cica.cica.indiana.edu (Frank Burleigh) writes:
>   Can the standard qsort accept not the array of structures, but
>   the array of pointers to structure members?

qsort() doesn't know what the array it is sorting consists of;
it leaves that up to the comparison function.  Thus, a standard
trick is to have qsort() sort an array of pointers to records
rather than an array of records.  The rest is merely a matter
of getting the type declarations right; this amounts to adding
an extra * to the parameters in the comparison function.  Note
that the comparison function is fed "generic" pointers (void* or
char*), and you ought to cast them into record_type**s before
using them in the comparison function.



More information about the Comp.lang.c mailing list