summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorotto <>2019-01-21 20:34:14 +0000
committerotto <>2019-01-21 20:34:14 +0000
commitc4e4e3af5f876df655f87ec686c99ca0e2f02c5e (patch)
treebd0e34e2fd71e25be45bc946f04a5f5238f40fba /src/lib
parent1d9efb961c55d10f321b2626df4b1ed77d104867 (diff)
downloadopenbsd-c4e4e3af5f876df655f87ec686c99ca0e2f02c5e.tar.gz
openbsd-c4e4e3af5f876df655f87ec686c99ca0e2f02c5e.tar.bz2
openbsd-c4e4e3af5f876df655f87ec686c99ca0e2f02c5e.zip
Add example showing a proper comparison function, as many examples show
the wrong idiom. ok tedu@ but probably needs some tweakin
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/stdlib/qsort.343
1 files changed, 40 insertions, 3 deletions
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3
index 29a29f3a4d..cadfda7961 100644
--- a/src/lib/libc/stdlib/qsort.3
+++ b/src/lib/libc/stdlib/qsort.3
@@ -29,9 +29,9 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" $OpenBSD: qsort.3,v 1.20 2017/05/20 13:09:01 millert Exp $ 32.\" $OpenBSD: qsort.3,v 1.21 2019/01/21 20:34:14 otto Exp $
33.\" 33.\"
34.Dd $Mdocdate: May 20 2017 $ 34.Dd $Mdocdate: January 21 2019 $
35.Dt QSORT 3 35.Dt QSORT 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
@@ -85,7 +85,7 @@ a comparison function pointed to by
85which requires two arguments pointing to the objects being 85which requires two arguments pointing to the objects being
86compared. 86compared.
87.Pp 87.Pp
88The comparison function must return an integer less than, equal to, or 88The comparison function must return an int less than, equal to, or
89greater than zero if the first argument is considered to be respectively 89greater than zero if the first argument is considered to be respectively
90less than, equal to, or greater than the second. 90less than, equal to, or greater than the second.
91.Pp 91.Pp
@@ -168,6 +168,43 @@ or
168.Fn mergesort 168.Fn mergesort
169were unable to allocate memory. 169were unable to allocate memory.
170.El 170.El
171.Sh EXAMPLES
172.Bd -literal
173#include <stdio.h>
174#include <stdlib.h>
175#include <string.h>
176
177char *array[] = { "XX", "YYY", "Z" };
178#define N (sizeof(array) / sizeof(array[0]))
179
180int
181cmp(const void *a, const void *b)
182{
183 /* a and b point to an element of the array */
184 size_t lena = strlen(*(const char **)a);
185 size_t lenb = strlen(*(const char **)b);
186 /*
187 * Do not subtract the lengths, an int cannot represent the range of
188 * values the difference can take.
189 */
190 return lena < lenb ? -1 : lena > lenb;
191}
192
193int
194main()
195{
196 int i;
197
198 qsort(array, N, sizeof(array[0]), cmp);
199 for (i = 0; i < N; i++)
200 printf("%s\n", array[i]);
201}
202
203
204.Ed
205.Pp
206It almost always an error to use subtraction to compute the return value
207of the comparison function.
171.Sh SEE ALSO 208.Sh SEE ALSO
172.Xr sort 1 , 209.Xr sort 1 ,
173.Xr radixsort 3 210.Xr radixsort 3