qsort mistakes in 4.0

forsyth at minster.york.ac.uk forsyth at minster.york.ac.uk
Thu Dec 21 00:21:46 AEST 1989


Someone added a little example to the manual page for qsort between 3.5
and 4.0.  unfortunately, it is wrong in every important aspect if one is
concerned about portability:

	- the comparison function must be declared to take
	  char * pointers (`const void *' in ansi):
		int compar(a, b)
		char *a, *b;
		{
			return *(int *)a - *(int *)b;
		}

	- the array must be cast to char * (or void *):
		qsort((char *)a, ... );

the example as shown will often fail on machines that have different
representations for word and character pointers (for instance).  it is not
type correct.  bsearch(3), and perhaps others, suffers from the same
fault.



More information about the Comp.sys.sun mailing list