Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!rpi!zaphod.mps.ohio-state.edu!mips!pacbell.com!att!cbnewsj!asd From: asd@cbnewsj.att.com (Adam S. Denton) Newsgroups: comp.lang.c Subject: Re: qsort() Message-ID: <1991Apr22.174958.15420@cbnewsj.att.com> Date: 22 Apr 91 17:49:58 GMT References: <1991Apr22.002627.14535@engin.umich.edu> Organization: AT&T Lines: 24 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