]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/qsort.3
man*/: srcfix (Use .P instead of .PP or .LP)
[thirdparty/man-pages.git] / man3 / qsort.3
CommitLineData
a1eaacb1 1'\" t
fea681da
MK
2.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
5.\"
6.\" References consulted:
7.\" Linux libc source code
8.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9.\" 386BSD man pages
10.\"
11.\" Modified 1993-03-29, David Metcalfe
12.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
788be124 13.\" 2006-01-15, mtk, Added example program.
6d388dc9
MK
14.\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net>
15.\" and Ben Bacarisse <software@bsb.me.uk>
16.\" Document qsort_r()
fea681da 17.\"
a5ebdc8d 18.TH qsort 3 (date) "Linux man-pages (unreleased)"
fea681da 19.SH NAME
b2db77f5 20qsort, qsort_r \- sort an array
b27c428d
AC
21.SH LIBRARY
22Standard C library
8fc3b2cf 23.RI ( libc ", " \-lc )
fea681da
MK
24.SH SYNOPSIS
25.nf
26.B #include <stdlib.h>
c6d039a3 27.P
c64cd13e
AC
28.BI "void qsort(void " base [. size " * ." nmemb "], size_t " nmemb ", \
29size_t " size ,
30.BI " int (*" compar ")(const void [." size "], \
31const void [." size ]));
32.BI "void qsort_r(void " base [. size " * ." nmemb "], size_t " nmemb ", \
33size_t " size ,
34.BI " int (*" compar ")(const void [." size "], \
35const void [." size "], void *),"
a094fe6d 36.BI " void *" arg ");"
fea681da 37.fi
c6d039a3 38.P
d39ad78f 39.RS -4
b2db77f5
BB
40Feature Test Macro Requirements for glibc (see
41.BR feature_test_macros (7)):
d39ad78f 42.RE
c6d039a3 43.P
b2db77f5 44.BR qsort_r ():
9d2adbae
MK
45.nf
46 _GNU_SOURCE
47.fi
fea681da 48.SH DESCRIPTION
60a90ecd
MK
49The
50.BR qsort ()
51function sorts an array with \fInmemb\fP elements of
c13182ef
MK
52size \fIsize\fP.
53The \fIbase\fP argument points to the start of the
fea681da 54array.
c6d039a3 55.P
fea681da
MK
56The contents of the array are sorted in ascending order according to a
57comparison function pointed to by \fIcompar\fP, which is called with two
58arguments that point to the objects being compared.
c6d039a3 59.P
fea681da
MK
60The comparison function must return an integer less than, equal to, or
61greater than zero if the first argument is considered to be respectively
c13182ef 62less than, equal to, or greater than the second.
6d388dc9
MK
63If two members compare as equal,
64their order in the sorted array is undefined.
c6d039a3 65.P
a094fe6d
MB
66The
67.BR qsort_r ()
68function is identical to
69.BR qsort ()
70except that the comparison function
71.I compar
6d388dc9
MK
72takes a third argument.
73A pointer is passed to the comparison function via
a094fe6d
MB
74.IR arg .
75In this way, the comparison function does not need to use global variables to
6d388dc9 76pass through arbitrary arguments, and is therefore reentrant and safe to
a094fe6d 77use in threads.
47297adb 78.SH RETURN VALUE
60a90ecd
MK
79The
80.BR qsort ()
a094fe6d
MB
81and
82.BR qsort_r ()
83functions return no value.
b24d9e5a
ZL
84.SH ATTRIBUTES
85For an explanation of the terms used in this section, see
86.BR attributes (7).
87.TS
88allbox;
c466875e 89lbx lb lb
b24d9e5a
ZL
90l l l.
91Interface Attribute Value
92T{
9e54434e
BR
93.na
94.nh
b24d9e5a
ZL
95.BR qsort (),
96.BR qsort_r ()
97T} Thread safety MT-Safe
98.TE
3113c7f3 99.SH STANDARDS
4131356c
AC
100.TP
101.BR qsort ()
102C11, POSIX.1-2008.
103.SH HISTORY
104.TP
105.BR qsort ()
106POSIX.1-2001, C89, SVr4, 4.3BSD.
107.TP
108.BR qsort_r ()
109glibc 2.8.
19c98696 110.SH NOTES
c13182ef 111To compare C strings, the comparison function can call
fb186734 112.BR strcmp (3),
cb478668 113as shown in the example below.
a14af333 114.SH EXAMPLES
788be124
MK
115For one example of use, see the example under
116.BR bsearch (3).
c6d039a3 117.P
a057d61f 118Another example is the following program,
48a1cb49 119which sorts the strings given in its command-line arguments:
c6d039a3 120.P
b0b6ab4e 121.\" SRC BEGIN (qsort.c)
bdd915e2 122.EX
788be124
MK
123#include <stdio.h>
124#include <stdlib.h>
788be124 125#include <string.h>
fe5dba13 126\&
788be124
MK
127static int
128cmpstringp(const void *p1, const void *p2)
129{
7c5bd0eb 130 /* The actual arguments to this function are "pointers to
9daa4fb9 131 pointers to char", but strcmp(3) arguments are "pointers
46b20ca1 132 to char", hence the following cast plus dereference. */
fe5dba13 133\&
9ba82f22 134 return strcmp(*(const char **) p1, *(const char **) p2);
788be124 135}
fe5dba13 136\&
788be124
MK
137int
138main(int argc, char *argv[])
139{
6b34fb3f 140 if (argc < 2) {
d1a71985 141 fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
5a6194a4 142 exit(EXIT_FAILURE);
6b34fb3f 143 }
fe5dba13 144\&
29059a65 145 qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
fe5dba13 146\&
b42296e4 147 for (size_t j = 1; j < argc; j++)
788be124
MK
148 puts(argv[j]);
149 exit(EXIT_SUCCESS);
150}
bdd915e2 151.EE
b0b6ab4e 152.\" SRC END
47297adb 153.SH SEE ALSO
fea681da
MK
154.BR sort (1),
155.BR alphasort (3),
156.BR strcmp (3),
157.BR versionsort (3)