Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!medar!jseymour From: jseymour@medar.com (James Seymour) Newsgroups: comp.lang.c Subject: Re: qsort() Message-ID: <114@hdwr1.medar.com> Date: 23 Apr 91 22:35:39 GMT References: <1991Apr22.002627.14535@engin.umich.edu> <1991Apr22.174958.15420@cbnewsj.att.com> Organization: Medar, Inc. Farmington Hills, MI Lines: 36 In article <1991Apr22.174958.15420@cbnewsj.att.com> asd@cbnewsj.att.com (Adam S. Denton) writes: :In article <1991Apr22.002627.14535@engin.umich.edu> garath@irie.ais.org (Belgarath) writes: ::qsort((char *) info, 49, sizeof(the_srtuct), compare); :: ::int compare(ptr1, ptr2) ::struct the_struct *ptr1; ::struct the_struct *ptr2; ::{ :: return (strcmp(ptr1->name, ptr2->name)); ::} : :Not quite. This should really be : int compare(ptr1,ptr2) : char *ptr1, *ptr2; : { : return strcmp( ((struct the_struct *)ptr1)->name, : ((struct the_struct *)ptr2)->name ); : } :and in an ANSI environment, "void *" should be used instead of "char *". : :qsort() passes generic pointers to compare(), not struct pointers. :There is no guarantee the two pointer types are interchangeable. : :Adam Denton :asd@mtqua.att.com Mea culpa! As Adam (and in another article, Dave Eisen) correctly pointed out, the function called by qsort() *should* have it's parms declared as char*/void*. Being one of those that mailed qsort pointers (no pun intended) to Belgarath, I thought it only fair to own up to my snafu. -- Jim Seymour | Medar, Inc. ...!uunet!medar!jseymour | 38700 Grand River Ave. jseymour@medar.com | Farmington Hills, MI. 48331 CIS: 72730,1166 GEnie: jseymour | FAX: (313)477-8897