Comparison functions for qsort() and bsearch()

Barry Margolin barmar at think.com
Wed Dec 26 08:35:44 AEST 1990


In article <277640DD.4A70 at tct.uucp> chip at tct.uucp (Chip Salzenberg) writes:
>>In short, IT IS IMPOSSIBLE TO WRITE QSORT() ENTIRELY IN ANSI C OR IN C++
>>IN SUCH A WAY THAT IT IS COMPLETELY PORTABLE.
>This statement is false.  It is entirely possible to do so, thanks to
>ANSI's guarantee that pointer-to-char and pointer-to-void will always
>have identical representations.

I think Chip is correct about C, but not necessarily about C++.  Actually,
qsort() itself can be written portably in C++, but it's hard to write most
comparison functions portably.  The problem is that C++ has restrictions on
casting from void* into <type>*.  So, you can pass the void* parameters to
the comparison function, but it may not be able to do anything with them.

--
Barry Margolin, Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list