Xref: utzoo comp.lang.c:25896 comp.unix.xenix:10067 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!umich!yale!cmcl2!rutgers!bpa!sjuphil!ryan From: ryan@sjuphil.uucp (Patrick M. Ryan) Newsgroups: comp.lang.c,comp.unix.xenix Subject: Re: qsort() - HELP Message-ID: <1990Feb13.204014.23338@sjuphil.uucp> Date: 13 Feb 90 20:40:14 GMT References: <5916@ozdaltx.UUCP> Reply-To: ryan@sjuphil.UUCP (Patrick M. Ryan) Organization: SJU Grad CSC Lines: 53 In article <5916@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes: >After going over the manuals more times than I'd like, I still can't >figure out how to get qsort() (S) to operate. The example shows >something like: > > void qsort(base, nel, width, compar) > char *base > unsigned nel, width > int (*compar)() > >Is base supposed to be an array? >nel & width are self-explanitory >What is compar() looking for or how is it assigned? > >The objective is to sort an array of strings in alpha order and then >be able to read them. So far I'm getting is core dumps. yes, base is supposed to be an array. compar() takes two pointers to structures to compare. compare must return an integer (say, ret) to indicate the order of the two items. ret = 0 if they are equal, ret < 0 if the first item is less than the second, and ret > 1 other wise. ex: #define LEN 32 #define MAX 512 char str[LEN][MAX]; int compare(); main() { . . . qsort(str,MAX,LEN,compare); . . } int compare(s1,s2) char *s1, *s2; { return(strcmp(s1,s2)); } -- patrick m. ryan ryan%sjuphil.sju.edu@bpa.bell-atl.com {bpa|burdvax|princeton|rutgers}!sjuphil!ryan pmr@gemini.gsfc.nasa.gov