summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdlib/qsort.3
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2019-01-21 20:43:27 +0000
committertedu <tedu@openbsd.org>2019-01-21 20:43:27 +0000
commit0a64a7dfef7fb4172436ec5dc566128704ca52f6 (patch)
treefed9857980e1f00052b6d8afb9f3ae4180207d60 /lib/libc/stdlib/qsort.3
parentAdd example showing a proper comparison function, as many examples show (diff)
downloadwireguard-openbsd-0a64a7dfef7fb4172436ec5dc566128704ca52f6.tar.xz
wireguard-openbsd-0a64a7dfef7fb4172436ec5dc566128704ca52f6.zip
a few tweaks
Diffstat (limited to 'lib/libc/stdlib/qsort.3')
-rw-r--r--lib/libc/stdlib/qsort.314
1 files changed, 9 insertions, 5 deletions
diff --git a/lib/libc/stdlib/qsort.3 b/lib/libc/stdlib/qsort.3
index cadfda79610..cbb1ec44d4f 100644
--- a/lib/libc/stdlib/qsort.3
+++ b/lib/libc/stdlib/qsort.3
@@ -29,7 +29,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $OpenBSD: qsort.3,v 1.21 2019/01/21 20:34:14 otto Exp $
+.\" $OpenBSD: qsort.3,v 1.22 2019/01/21 20:43:27 tedu Exp $
.\"
.Dd $Mdocdate: January 21 2019 $
.Dt QSORT 3
@@ -180,12 +180,16 @@ char *array[] = { "XX", "YYY", "Z" };
int
cmp(const void *a, const void *b)
{
- /* a and b point to an element of the array */
+ /*
+ * a and b point to elements of the array.
+ * Cast and dereference to obtain the actual elements,
+ * which are also pointers in this case.
+ */
size_t lena = strlen(*(const char **)a);
size_t lenb = strlen(*(const char **)b);
/*
- * Do not subtract the lengths, an int cannot represent the range of
- * values the difference can take.
+ * Do not subtract the lengths. The difference between values cannot
+ * be represented by an int.
*/
return lena < lenb ? -1 : lena > lenb;
}
@@ -193,7 +197,7 @@ cmp(const void *a, const void *b)
int
main()
{
- int i;
+ size_t i;
qsort(array, N, sizeof(array[0]), cmp);
for (i = 0; i < N; i++)