Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uwm.edu!bionet!agate!stanford.edu!leland.Stanford.EDU!dkeisen From: dkeisen@leland.Stanford.EDU (Dave Eisen) Newsgroups: comp.lang.c Subject: Re: qsort() Message-ID: <1991Apr22.160402.22362@leland.Stanford.EDU> Date: 22 Apr 91 16:04:02 GMT References: <1991Apr22.002627.14535@engin.umich.edu> Organization: Sequoia Peripherals, Inc. Lines: 39 In article <1991Apr22.002627.14535@engin.umich.edu> garath@irie.ais.org (Belgarath) writes: > > Thanks to everyone who mailed me with the solution to how to use >qsort(). Basically after you have defined the structure you do: > >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)); >} Nope. The comparison function takes two "generic pointer" parameters, (void * in ANSI C, char * in Classic C), not parameters of some random type the implementor of qsort has never heard of. These parameters should then be cast to the appropriate type in your comparison function. Something like (since you seem to be using classic C): int compare (ptr1, ptr2) char *ptr1; char *ptr2; { return (strcmp (((struct the_struct *) ptr1)->name, ((struct the_struct *) ptr1)->name)); } -- Dave Eisen dkeisen@leland.Stanford.EDU 1101 San Antonio Raod, Suite 102 (Gang-of-Four is being taken off the net) Mountain View, CA 94043 (415) 967-5644