mirror of https://github.com/mkerrisk/man-pages
Clarify how strcmp() should be used as the 'compar'
function by providing an example.
This commit is contained in:
parent
ab95e95e01
commit
788be12467
44
man3/qsort.3
44
man3/qsort.3
|
@ -27,6 +27,7 @@
|
||||||
.\"
|
.\"
|
||||||
.\" Modified 1993-03-29, David Metcalfe
|
.\" Modified 1993-03-29, David Metcalfe
|
||||||
.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
|
.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
|
||||||
|
.\" 2006-01-15, mtk, Added example program.
|
||||||
.\"
|
.\"
|
||||||
.TH QSORT 3 2003-11-15 "" "Linux Programmer's Manual"
|
.TH QSORT 3 2003-11-15 "" "Linux Programmer's Manual"
|
||||||
.SH NAME
|
.SH NAME
|
||||||
|
@ -61,14 +62,49 @@ SVID 3, POSIX, 4.3BSD, ISO 9899
|
||||||
Library routines suitable for use as the
|
Library routines suitable for use as the
|
||||||
.I compar
|
.I compar
|
||||||
argument include
|
argument include
|
||||||
.BR strcmp (),
|
.BR strcmp ()
|
||||||
|
(see below),
|
||||||
.BR alphasort (),
|
.BR alphasort (),
|
||||||
and
|
and
|
||||||
.BR versionsort ().
|
.BR versionsort ().
|
||||||
.SH EXAMPLE
|
.SH EXAMPLE
|
||||||
For an example of use, see the example on the
|
For one example of use, see the example under
|
||||||
.BR bsearch (3)
|
.BR bsearch (3).
|
||||||
page.
|
|
||||||
|
The following example program sorts the strings given
|
||||||
|
in its command-line arguments:
|
||||||
|
.sp
|
||||||
|
.nf
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
static int
|
||||||
|
cmpstringp(const void *p1, const void *p2)
|
||||||
|
{
|
||||||
|
/* The arguments to this function are "pointers to
|
||||||
|
pointers to char", but strcmp() arguments are "pointers
|
||||||
|
to char", hence the following cast plus dereference */
|
||||||
|
|
||||||
|
return strcmp(* (char **) p1, * (char **) p2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int j;
|
||||||
|
|
||||||
|
assert(argc > 1);
|
||||||
|
|
||||||
|
qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
|
||||||
|
|
||||||
|
for (j = 1; j < argc; j++)
|
||||||
|
puts(argv[j]);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
.fi
|
||||||
.SH "SEE ALSO"
|
.SH "SEE ALSO"
|
||||||
.BR sort (1),
|
.BR sort (1),
|
||||||
.BR alphasort (3),
|
.BR alphasort (3),
|
||||||
|
|
Loading…
Reference in New Issue