Author: kib
Date: Wed Jul 24 06:29:19 2019
New Revision: 350272
URL: https://svnweb.freebsd.org/changeset/base/350272

Log:
  MFC r350091:
  bsearch.3: Improve the example

Modified:
  stable/12/lib/libc/stdlib/bsearch.3
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/lib/libc/stdlib/bsearch.3
==============================================================================
--- stable/12/lib/libc/stdlib/bsearch.3 Wed Jul 24 04:48:04 2019        
(r350271)
+++ stable/12/lib/libc/stdlib/bsearch.3 Wed Jul 24 06:29:19 2019        
(r350272)
@@ -32,7 +32,7 @@
 .\"     @(#)bsearch.3  8.3 (Berkeley) 4/19/94
 .\" $FreeBSD$
 .\"
-.Dd May 15, 2019
+.Dd July 17, 2019
 .Dt BSEARCH 3
 .Os
 .Sh NAME
@@ -93,26 +93,29 @@ A sample program that searches people by age in a sort
 #include <string.h>
 
 struct person {
-       char name[5];
-       int age;
+       const char      *name;
+       int             age;
 };
 
-int
-compare(const void *key, const void *array_member)
+static int
+compare(const void *a, const void *b)
 {
-       int age = (intptr_t) key;
-       struct person person = *(struct person *) array_member;
+       const int *age;
+       const struct person *person;
 
-       return (age - person.age);
+       age = a;
+       person = b;
+
+       return (*age - person->age);
 }
 
 int
-main()
+main(void)
 {
        struct person *friend;
-
+       int age;
        /* Sorted array */
-       struct person friends[6] = {
+       const struct person friends[] = {
                { "paul", 22 },
                { "anne", 25 },
                { "fred", 25 },
@@ -120,22 +123,28 @@ main()
                { "mark", 35 },
                { "bill", 50 }
        };
+       const size_t len = sizeof(friends) / sizeof(friends[0]);
 
-       size_t array_size = sizeof(friends) / sizeof(struct person);
-
-       friend = bsearch((void *)22, &friends, array_size, sizeof(struct 
person), compare);
+       age = 22;
+       friend = bsearch(&age, friends, len, sizeof(friends[0]), compare);
        assert(strcmp(friend->name, "paul") == 0);
        printf("name: %s\enage: %d\en", friend->name, friend->age);
 
-       friend = bsearch((void *)25, &friends, array_size, sizeof(struct 
person), compare);
-       assert(strcmp(friend->name, "fred") == 0 || strcmp(friend->name, 
"anne") == 0);
+       age = 25;
+       friend = bsearch(&age, friends, len, sizeof(friends[0]), compare);
+
+       /*
+        * For multiple elements with the same key, it is implementation
+        * defined which will be returned
+        */
+       assert(strcmp(friend->name, "fred") == 0 ||
+           strcmp(friend->name, "anne") == 0);
        printf("name: %s\enage: %d\en", friend->name, friend->age);
 
-       friend = bsearch((void *)30, &friends, array_size, sizeof(struct 
person), compare);
+       age = 30;
+       friend = bsearch(&age, friends, len, sizeof(friends[0]), compare);
        assert(friend == NULL);
        printf("friend aged 30 not found\en");
-
-       return (EXIT_SUCCESS);
 }
 .Ed
 .Sh SEE ALSO
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to