home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16024 < prev    next >
Encoding:
Text File  |  1992-11-05  |  2.5 KB  |  56 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!leland.Stanford.EDU!dkeisen
  3. From: dkeisen@leland.Stanford.EDU (Dave Eisen)
  4. Subject: Re: Help.  (Unix C)
  5. Message-ID: <1992Nov5.154301.27354@leland.Stanford.EDU>
  6. Sender: news@leland.Stanford.EDU (Mr News)
  7. Organization: Sequoia Peripherals, Inc.
  8. References: <2214@sdrc.COM> <Bx8Cw8.5tB@portal.hq.videocart.com>
  9. Date: Thu, 5 Nov 92 15:43:01 GMT
  10. Lines: 44
  11.  
  12. In article <Bx8Cw8.5tB@portal.hq.videocart.com> dfuller@portal.hq.videocart.com (Dave Fuller) writes:
  13.  
  14. [ Stuff about calling qsort with a comparison function whose arguments
  15.   are of type FOO *, not void * ]
  16.  
  17. >And if the code were not portable, it compiles and works on a non-ansi
  18. >compiler (and all of the ANSI ones i tried), so please take back the
  19. >part where you say the code is not correct. it is.
  20.  
  21. The code is not correct. qsort calls the function expecting 
  22. to pass two (void *) (actually two const void *) parameters, 
  23. therefore your comparison function must take parameters of 
  24. that type. 
  25.  
  26. The only way a (void *) is special is that you can assign a 
  27. (FOO *) to a (void *) and back without any problems; this says 
  28. nothing about whether you can pass (void *) actuals to a function 
  29. which takes parameters of type (FOO *). And in fact you can't do 
  30. this: both Classic C and ANSI C require all function calls to be 
  31. made with actual parameters and formal parameters of the same types. 
  32. Any deviation from this gives undefined behavior. Yet this is exactly
  33. what happens when you use a comparison function in qsort that takes
  34. arguments of type (FOO *).
  35.  
  36. As you remarked, a lot of code uses strcmp as a comparison function.
  37. All I can say is that there is a lot of bad code out there (or a lot
  38. of code written to work in only an environment where this is in fact
  39. guaranteed to work). Although this might be an exception: the ANSI C 
  40. standard requires that (void *) and (char *) have the same representation 
  41. so one might be able to weasel out with a function that takes parameters
  42. of type (char *). I'll leave that to the language lawyers out here to 
  43. tell us. 
  44.  
  45. This case (strcmp used as a comparison function) still has nothing to 
  46. do with your example where you tried to use a (struct foo *) instead 
  47. of a (void *). This example is just plain wrong.
  48.  
  49.  
  50.  
  51. -- 
  52. Dave Eisen                               Sequoia Peripherals: (415) 967-5644
  53. dkeisen@leland.Stanford.EDU              Home:                (415) 321-5154
  54.        There's something in my library to offend everybody. 
  55.           --- Washington Coalition Against Censorship
  56.