char* vs void* (was: Comparison functions for qsort() and bsearch())

Chip Salzenberg chip at tct.uucp
Thu Dec 27 00:37:24 AEST 1990


According to ark at alice.UUCP ():
>In article <277640DD.4A70 at tct.uucp> chip at tct.uucp (Chip Salzenberg) writes:
>> It is entirely possible to [write a portable implementation of qsort()],
>> thanks to ANSI's guarantee that pointer-to-char and pointer-to-void will
>> always have identical representations.
>
>How's that again?  Why do you think that char* and void* have to have
>identical representations?

The ANSI C standard requires that |char *| and |void *| have identical
representations.  This requirement bows to existing practice.  After
all, before ANSI came along, the return type of malloc() was |char *|.
So pre-ANSI C's generic pointer type was, of necessity, |char *|.

The section of E&S discussing differences between C and C++ makes no
mention of any change to this requirement; thus it is part of C++ too.

This equivalence is important because pointers of type |void *| do not
support the pointer arithmetic required to access and rearrange the
elements of the array to be sorted.  Such pointer arithmetic requires
the use of |char *| values.  The equivalence guarantee means that a
portable qsort() may convert pointers from |void *| to |char *| and
back without fear of loss of information.
-- 
Chip Salzenberg at Teltronics/TCT     <chip at tct.uucp>, <uunet!pdn!tct!chip>
"Please don't send me any more of yer scandalous email, Mr. Salzenberg..."
		-- Bruce Becker



More information about the Comp.lang.c mailing list