OSDN Git Service

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