]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/qsort.3
man*/: ffix (un-bracket tables)
[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).
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
847e0d88 99.sp 1
3113c7f3 100.SH STANDARDS
4131356c
AC
101.TP
102.BR qsort ()
103C11, POSIX.1-2008.
104.SH HISTORY
105.TP
106.BR qsort ()
107POSIX.1-2001, C89, SVr4, 4.3BSD.
108.TP
109.BR qsort_r ()
110glibc 2.8.
19c98696 111.SH NOTES
c13182ef 112To compare C strings, the comparison function can call
fb186734 113.BR strcmp (3),
cb478668 114as shown in the example below.
a14af333 115.SH EXAMPLES
788be124
MK
116For one example of use, see the example under
117.BR bsearch (3).
847e0d88 118.PP
a057d61f 119Another example is the following program,
48a1cb49 120which sorts the strings given in its command-line arguments:
bdd915e2 121.PP
b0b6ab4e 122.\" SRC BEGIN (qsort.c)
bdd915e2 123.EX
788be124
MK
124#include <stdio.h>
125#include <stdlib.h>
788be124 126#include <string.h>
fe5dba13 127\&
788be124
MK
128static int
129cmpstringp(const void *p1, const void *p2)
130{
7c5bd0eb 131 /* The actual arguments to this function are "pointers to
9daa4fb9 132 pointers to char", but strcmp(3) arguments are "pointers
46b20ca1 133 to char", hence the following cast plus dereference. */
fe5dba13 134\&
9ba82f22 135 return strcmp(*(const char **) p1, *(const char **) p2);
788be124 136}
fe5dba13 137\&
788be124
MK
138int
139main(int argc, char *argv[])
140{
6b34fb3f 141 if (argc < 2) {
d1a71985 142 fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
5a6194a4 143 exit(EXIT_FAILURE);
6b34fb3f 144 }
fe5dba13 145\&
29059a65 146 qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
fe5dba13 147\&
b42296e4 148 for (size_t j = 1; j < argc; j++)
788be124
MK
149 puts(argv[j]);
150 exit(EXIT_SUCCESS);
151}
bdd915e2 152.EE
b0b6ab4e 153.\" SRC END
47297adb 154.SH SEE ALSO
fea681da
MK
155.BR sort (1),
156.BR alphasort (3),
157.BR strcmp (3),
158.BR versionsort (3)