]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/qsort.3
getent.1, intro.1, time.1, _exit.2, _syscall.2, accept.2, access.2, acct.2, adjtimex...
[thirdparty/man-pages.git] / man3 / qsort.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
c13182ef 11.\"
fea681da
MK
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
c13182ef 19.\"
fea681da
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" References consulted:
24.\" Linux libc source code
25.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26.\" 386BSD man pages
27.\"
28.\" Modified 1993-03-29, David Metcalfe
29.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
788be124 30.\" 2006-01-15, mtk, Added example program.
6d388dc9
MK
31.\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net>
32.\" and Ben Bacarisse <software@bsb.me.uk>
33.\" Document qsort_r()
fea681da 34.\"
6d388dc9 35.TH QSORT 3 2012-03-08 "" "Linux Programmer's Manual"
fea681da 36.SH NAME
b2db77f5 37qsort, qsort_r \- sort an array
fea681da
MK
38.SH SYNOPSIS
39.nf
40.B #include <stdlib.h>
41.sp
42.BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
b2db77f5 43.BI " int (*" compar ")(const void *, const void *));"
a094fe6d
MB
44.sp
45.BI "void qsort_r(void *" base ", size_t " nmemb ", size_t " size ,
b2db77f5 46.BI " int (*" compar ")(const void *, const void *, void *),"
a094fe6d 47.BI " void *" arg ");"
fea681da 48.fi
b2db77f5
BB
49.sp
50.in -4n
51Feature Test Macro Requirements for glibc (see
52.BR feature_test_macros (7)):
53.in
54.sp
55.ad l
56.BR qsort_r ():
57_GNU_SOURCE
58.ad b
fea681da 59.SH DESCRIPTION
60a90ecd
MK
60The
61.BR qsort ()
62function sorts an array with \fInmemb\fP elements of
c13182ef
MK
63size \fIsize\fP.
64The \fIbase\fP argument points to the start of the
fea681da
MK
65array.
66.PP
67The contents of the array are sorted in ascending order according to a
68comparison function pointed to by \fIcompar\fP, which is called with two
69arguments that point to the objects being compared.
70.PP
71The comparison function must return an integer less than, equal to, or
72greater than zero if the first argument is considered to be respectively
c13182ef 73less than, equal to, or greater than the second.
6d388dc9
MK
74If two members compare as equal,
75their order in the sorted array is undefined.
a094fe6d
MB
76.PP
77The
78.BR qsort_r ()
79function is identical to
80.BR qsort ()
81except that the comparison function
82.I compar
6d388dc9
MK
83takes a third argument.
84A pointer is passed to the comparison function via
a094fe6d
MB
85.IR arg .
86In this way, the comparison function does not need to use global variables to
6d388dc9 87pass through arbitrary arguments, and is therefore reentrant and safe to
a094fe6d 88use in threads.
47297adb 89.SH RETURN VALUE
60a90ecd
MK
90The
91.BR qsort ()
a094fe6d
MB
92and
93.BR qsort_r ()
94functions return no value.
3b52faf1
MK
95.SH VERSIONS
96.BR qsort_r ()
97was added to glibc in version 2.8.
47297adb 98.SH CONFORMING TO
a094fe6d
MB
99The
100.BR qsort ()
101function conforms to SVr4, 4.3BSD, C89, C99.
19c98696 102.SH NOTES
fea681da
MK
103Library routines suitable for use as the
104.I compar
a094fe6d
MB
105argument to
106.BR qsort ()
107include
fb186734 108.BR alphasort (3)
fea681da 109and
fb186734 110.BR versionsort (3).
c13182ef 111To compare C strings, the comparison function can call
fb186734 112.BR strcmp (3),
cb478668 113as shown in the example below.
fea681da 114.SH EXAMPLE
788be124
MK
115For one example of use, see the example under
116.BR bsearch (3).
117
a057d61f 118Another example is the following program,
48a1cb49 119which sorts the strings given in its command-line arguments:
788be124
MK
120.sp
121.nf
122#include <stdio.h>
123#include <stdlib.h>
788be124 124#include <string.h>
788be124
MK
125
126static int
127cmpstringp(const void *p1, const void *p2)
128{
7c5bd0eb 129 /* The actual arguments to this function are "pointers to
9daa4fb9 130 pointers to char", but strcmp(3) arguments are "pointers
788be124
MK
131 to char", hence the following cast plus dereference */
132
7c5bd0eb 133 return strcmp(* (char * const *) p1, * (char * const *) p2);
788be124
MK
134}
135
136int
137main(int argc, char *argv[])
138{
139 int j;
140
6b34fb3f
MK
141 if (argc < 2) {
142 fprintf(stderr, "Usage: %s <string>...\\n", argv[0]);
143 exit(EXIT_FAILURE);
144 }
788be124 145
29059a65 146 qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
788be124
MK
147
148 for (j = 1; j < argc; j++)
149 puts(argv[j]);
150 exit(EXIT_SUCCESS);
151}
152.fi
47297adb 153.SH SEE ALSO
fea681da
MK
154.BR sort (1),
155.BR alphasort (3),
156.BR strcmp (3),
157.BR versionsort (3)