Casting Function Pointers

Norman Diamond diamond at jit533.swstokyo.dec.com
Fri May 24 10:50:25 AEST 1991


In article <1145 at mwtech.UUCP> martin at mwtech.UUCP (Martin Weitzel) writes:
>There has recently been some discussion about the correct way to
>write comparison functions for qsort.  There seems to be some
>consensus that the comparison function must be written expecting
>two pointers to void and eventually casting (or assigning) them
>internally to the required pointer type.

I'd agree with that.

>But have a look at the example in K&R-II, page 119-120

Sorry, I don't have one, but ...

>(Oh boy, was this hard to type in now, suspecting the fathers of C to
>have written something that might be wrong.)

There are a few errors and inconsistencies in K&R-I.  And actually I'm waiting
for K&R-III, because the proposed standard changed a little bit after K&R-II
and before finalization.

>	"The elaborate cast of the function argument casts the
>	arguments of the comparison function. These will generally
>	have no effect on actual representation but assure the
>	compiler that all is well."

This wording appears very sloppy, and as you said, gives wrong impressions.
It's not clear if the authors made a technical error (which is entirely
possible) in addition to sloppy wording.  (Their code might make it clear.)

>Third and most important: Will the guarantees ANSI C gives with
>respect to the (identical) representation of certain pointer types
>extend to the calling sequence of a function?

No.  A function with two parameters might have a different calling sequence
from a function with zero parameters.  A function with an int parameter might
have a different calling sequence from a function with a struct parameter.

>Does the identical representation of pointers to char and pointers to void
>extend to an identical calling sequence for
>  int f1(void *, void *)    and    int f2(char *, char *)  ?

I think yes.  The two function types are not compatible (by the definition
of compatible) but should be interchangeable (no definition here) because
of the identical representation of void* and char*.
--
Norman Diamond       diamond at tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
Permission is granted to feel this signature, but not to look at it.



More information about the Comp.std.c mailing list