home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!dkeisen
- From: dkeisen@leland.Stanford.EDU (Dave Eisen)
- Subject: Re: Help. (Unix C)
- Message-ID: <1992Nov5.154301.27354@leland.Stanford.EDU>
- Sender: news@leland.Stanford.EDU (Mr News)
- Organization: Sequoia Peripherals, Inc.
- References: <2214@sdrc.COM> <Bx8Cw8.5tB@portal.hq.videocart.com>
- Date: Thu, 5 Nov 92 15:43:01 GMT
- Lines: 44
-
- In article <Bx8Cw8.5tB@portal.hq.videocart.com> dfuller@portal.hq.videocart.com (Dave Fuller) writes:
-
- [ Stuff about calling qsort with a comparison function whose arguments
- are of type FOO *, not void * ]
-
- >And if the code were not portable, it compiles and works on a non-ansi
- >compiler (and all of the ANSI ones i tried), so please take back the
- >part where you say the code is not correct. it is.
-
- The code is not correct. qsort calls the function expecting
- to pass two (void *) (actually two const void *) parameters,
- therefore your comparison function must take parameters of
- that type.
-
- The only way a (void *) is special is that you can assign a
- (FOO *) to a (void *) and back without any problems; this says
- nothing about whether you can pass (void *) actuals to a function
- which takes parameters of type (FOO *). And in fact you can't do
- this: both Classic C and ANSI C require all function calls to be
- made with actual parameters and formal parameters of the same types.
- Any deviation from this gives undefined behavior. Yet this is exactly
- what happens when you use a comparison function in qsort that takes
- arguments of type (FOO *).
-
- As you remarked, a lot of code uses strcmp as a comparison function.
- All I can say is that there is a lot of bad code out there (or a lot
- of code written to work in only an environment where this is in fact
- guaranteed to work). Although this might be an exception: the ANSI C
- standard requires that (void *) and (char *) have the same representation
- so one might be able to weasel out with a function that takes parameters
- of type (char *). I'll leave that to the language lawyers out here to
- tell us.
-
- This case (strcmp used as a comparison function) still has nothing to
- do with your example where you tried to use a (struct foo *) instead
- of a (void *). This example is just plain wrong.
-
-
-
- --
- Dave Eisen Sequoia Peripherals: (415) 967-5644
- dkeisen@leland.Stanford.EDU Home: (415) 321-5154
- There's something in my library to offend everybody.
- --- Washington Coalition Against Censorship
-