qsort()
Dave Eisen
dkeisen at leland.Stanford.EDU
Tue Apr 23 02:04:02 AEST 1991
In article <1991Apr22.002627.14535 at engin.umich.edu> garath at 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 at 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
More information about the Comp.lang.c
mailing list