]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/qsort.3
*.mk, *.grep: Move grep patterns to .grep files
[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>
68e4db0a 27.PP
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
68e4db0a 38.PP
d39ad78f 39.RS -4
b2db77f5
BB
40Feature Test Macro Requirements for glibc (see
41.BR feature_test_macros (7)):
d39ad78f 42.RE
68e4db0a 43.PP
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
MK
54array.
55.PP
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.
59.PP
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.
a094fe6d
MB
65.PP
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).
c466875e
MK
87.ad l
88.nh
b24d9e5a
ZL
89.TS
90allbox;
c466875e 91lbx lb lb
b24d9e5a
ZL
92l l l.
93Interface Attribute Value
94T{
95.BR qsort (),
96.BR qsort_r ()
97T} Thread safety MT-Safe
98.TE
c466875e
MK
99.hy
100.ad
847e0d88 101.sp 1
3113c7f3 102.SH STANDARDS
4131356c
AC
103.TP
104.BR qsort ()
105C11, POSIX.1-2008.
106.SH HISTORY
107.TP
108.BR qsort ()
109POSIX.1-2001, C89, SVr4, 4.3BSD.
110.TP
111.BR qsort_r ()
112glibc 2.8.
19c98696 113.SH NOTES
c13182ef 114To compare C strings, the comparison function can call
fb186734 115.BR strcmp (3),
cb478668 116as shown in the example below.
a14af333 117.SH EXAMPLES
788be124
MK
118For one example of use, see the example under
119.BR bsearch (3).
847e0d88 120.PP
a057d61f 121Another example is the following program,
48a1cb49 122which sorts the strings given in its command-line arguments:
bdd915e2 123.PP
b0b6ab4e 124.\" SRC BEGIN (qsort.c)
bdd915e2 125.EX
788be124
MK
126#include <stdio.h>
127#include <stdlib.h>
788be124 128#include <string.h>
788be124
MK
129
130static int
131cmpstringp(const void *p1, const void *p2)
132{
7c5bd0eb 133 /* The actual arguments to this function are "pointers to
9daa4fb9 134 pointers to char", but strcmp(3) arguments are "pointers
46b20ca1 135 to char", hence the following cast plus dereference. */
788be124 136
9ba82f22 137 return strcmp(*(const char **) p1, *(const char **) p2);
788be124
MK
138}
139
140int
141main(int argc, char *argv[])
142{
6b34fb3f 143 if (argc < 2) {
d1a71985 144 fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
5a6194a4 145 exit(EXIT_FAILURE);
6b34fb3f 146 }
788be124 147
29059a65 148 qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
788be124 149
b42296e4 150 for (size_t j = 1; j < argc; j++)
788be124
MK
151 puts(argv[j]);
152 exit(EXIT_SUCCESS);
153}
bdd915e2 154.EE
b0b6ab4e 155.\" SRC END
47297adb 156.SH SEE ALSO
fea681da
MK
157.BR sort (1),
158.BR alphasort (3),
159.BR strcmp (3),
160.BR versionsort (3)