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