home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13684 < prev    next >
Encoding:
Internet Message Format  |  1992-09-15  |  2.0 KB

  1. Path: sparky!uunet!cs.utexas.edu!sun-barr!olivea!spool.mu.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uchinews!quads!pynq
  2. From: pynq@quads.uchicago.edu (George Jetson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problems with QSORT and function prototypes
  5. Message-ID: <1992Sep15.143458.21088@midway.uchicago.edu>
  6. Date: 15 Sep 92 14:34:58 GMT
  7. References: <1992Sep15.043445.3519@midway.uchicago.edu> <1992Sep15.123225.22760@blaze.cs.jhu.edu>
  8. Sender: news@uchinews.uchicago.edu (News System)
  9. Reply-To: pynq@midway.uchicago.edu
  10. Organization: D. J. Dougherty & Associates
  11. Lines: 38
  12.  
  13. In article <1992Sep15.123225.22760@blaze.cs.jhu.edu> wilson@rhombus.cs.jhu.edu (Dwight Wilson) writes:
  14.  
  15. >
  16. >qsort() is declared as:
  17. >
  18. >void qsort(void *base, size_t nmemb, size_t size,
  19. >           int (*compar)(const void *, const void *));
  20. >
  21. >Your problem is being caused by the fourth argument, the comparison
  22. >function.  qsort's declaration means that the comparison function
  23. >should take two (void *) parameters and return an int.
  24. >
  25. >Your functions expect two struct thing pointers or two int pointers.
  26. >You must rewrite them to take (void *) arguments (and cast them
  27. >internally).
  28.  
  29. In fact, to get it past Borland C, I had to declare them as "const void *" -
  30. void * alone was not enough.
  31.  
  32. Interestingly enough, the following hack worked as well:
  33. Declare:
  34.  
  35.     int (*fp)() = int_cmp;
  36.  
  37. Then use fp, not int_cmp, in the call to qsort().
  38.  
  39. I'm not sure which hack is less unattractive, but the fact that Borland
  40. treats this as an error, not merely a warning, seems a bit excessive to me.
  41.  
  42. ************************************************************************
  43. A note to new readers:  the weasely :-) symbol is frowned upon in this
  44. group.  Any questions, see Phil Gustafson who will gladly rearrange your
  45. diodes for you.
  46.             - From the AFU FAQ -
  47.  
  48.     - pynq@quads.uchicago.edu, who is still costing the net
  49.       hundreds, if not thousands, of dollars, every time he posts -
  50. ************************************************************************
  51.