"Oskar Liljeblad" <[EMAIL PROTECTED]> writes:

> Sure, but assume you're passing strcmp for the comparison function,
> wouldn't you want to cast it to avoid the warning?

I should warn you that the C Standard does not allow that sort of
cast.  This is for portability to hosts that use different
representations for different kinds of pointers; such hosts can use
different calling conventions for char * and void *, so casting the
function pointer will result in code that doesn't work.  Admittedly
such hosts are rare (typically they're word-oriented machines) but
all other things being equal we might as well port to them.

Personally I avoid this problem by using a shim, e.g.:

   static int actual_compare (my_type *a, my_type *b) { ... whatever ... }
   static int cmp (void *a, void *b) { return actual_compare (a, b); }
   ...
   qsort (array, nitems, sizeof *array, cmp);

This avoids the need for casts entirely (and will work on that old
Unisys or Cray mainframe :-).

Admittedly there's a performance hit in some cases but it's typically
so small I can't measure it.  And making "actual_compare" inline
shrinks the runtime overhead to zero.


_______________________________________________
bug-gnulib mailing list
bug-gnulib@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-gnulib

Reply via email to