summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Colomar <alx.manpages@gmail.com>2022-09-10 23:45:37 +0200
committerAlex Colomar <alx.manpages@gmail.com>2022-09-12 17:08:24 +0200
commit6a8012f16ac000b4fe04891cf6aacd229a556b4b (patch)
treef7566cfcd2411d3d68f91493be2a43828bc46366
parent8e6b55a7bee1b51ef7d6ab58f77023fca47625c4 (diff)
bsearch.3: EXAMPLES: Use ARRAY_SIZE()
This is more generic code, and hopefully, it will inspire other to use such a pattern. Signed-off-by: Alex Colomar <alx.manpages@gmail.com>
-rw-r--r--man3/bsearch.38
1 files changed, 4 insertions, 4 deletions
diff --git a/man3/bsearch.3 b/man3/bsearch.3
index cf5fa7a0a..dbb0a2fa5 100644
--- a/man3/bsearch.3
+++ b/man3/bsearch.3
@@ -88,6 +88,8 @@ then retrieves desired elements using
#include <stdlib.h>
#include <string.h>
+#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))
+
struct mi {
int nr;
const char *name;
@@ -99,8 +101,6 @@ static struct mi months[] = {
{ 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
};
-#define nr_of_months (sizeof(months)/sizeof(months[0]))
-
static int
compmi(const void *m1, const void *m2)
{
@@ -112,13 +112,13 @@ compmi(const void *m1, const void *m2)
int
main(int argc, char *argv[])
{
- qsort(months, nr_of_months, sizeof(months[0]), compmi);
+ qsort(months, ARRAY_SIZE(months), sizeof(months[0]), compmi);
for (int i = 1; i < argc; i++) {
struct mi key;
struct mi *res;
key.name = argv[i];
- res = bsearch(&key, months, nr_of_months,
+ res = bsearch(&key, months, ARRAY_SIZE(months),
sizeof(months[0]), compmi);
if (res == NULL)
printf("\(aq%s\(aq: unknown month\en", argv[i]);