qsort() - HELP
Patrick M. Ryan
ryan at sjuphil.uucp
Wed Feb 14 07:40:14 AEST 1990
In article <5916 at ozdaltx.UUCP> root at 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 at bpa.bell-atl.com
{bpa|burdvax|princeton|rutgers}!sjuphil!ryan
pmr at gemini.gsfc.nasa.gov
More information about the Comp.lang.c
mailing list