Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!decwrl!pa.dec.com!jrdzzz.jrd.dec.com!tkou02.enet.dec.com!jit533!diamond From: diamond@jit533.swstokyo.dec.com (Norman Diamond) Newsgroups: comp.std.c Subject: Re: Casting Function Pointers Message-ID: <1991May24.005025.7714@tkou02.enet.dec.com> Date: 24 May 91 00:50:25 GMT References: <1145@mwtech.UUCP> Sender: usenet@tkou02.enet.dec.com (USENET News System) Reply-To: diamond@jit533.enet@tkou02.enet.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo Lines: 48 In article <1145@mwtech.UUCP> martin@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@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.