qsort()

James Seymour jseymour at medar.com
Wed Apr 24 08:35:39 AEST 1991


In article <1991Apr22.174958.15420 at cbnewsj.att.com> asd at cbnewsj.att.com (Adam S. Denton) writes:
:In article <1991Apr22.002627.14535 at engin.umich.edu> garath at 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 at 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 at medar.com			| Farmington Hills, MI. 48331
CIS: 72730,1166  GEnie: jseymour	| FAX: (313)477-8897



More information about the Comp.lang.c mailing list