From owner-svn-src-stable@freebsd.org Sat May 18 03:15:09 2019 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D678C15A659E; Sat, 18 May 2019 03:15:08 +0000 (UTC) (envelope-from bcr@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 703C0952C3; Sat, 18 May 2019 03:15:08 +0000 (UTC) (envelope-from bcr@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4584819C23; Sat, 18 May 2019 03:15:08 +0000 (UTC) (envelope-from bcr@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x4I3F86i080554; Sat, 18 May 2019 03:15:08 GMT (envelope-from bcr@FreeBSD.org) Received: (from bcr@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x4I3F8g2080553; Sat, 18 May 2019 03:15:08 GMT (envelope-from bcr@FreeBSD.org) Message-Id: <201905180315.x4I3F8g2080553@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bcr set sender to bcr@FreeBSD.org using -f From: Benedict Reuschling Date: Sat, 18 May 2019 03:15:08 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r347951 - stable/12/lib/libc/stdlib X-SVN-Group: stable-12 X-SVN-Commit-Author: bcr X-SVN-Commit-Paths: stable/12/lib/libc/stdlib X-SVN-Commit-Revision: 347951 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 703C0952C3 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.95 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.95)[-0.954,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 18 May 2019 03:15:09 -0000 Author: bcr (doc committer) Date: Sat May 18 03:15:07 2019 New Revision: 347951 URL: https://svnweb.freebsd.org/changeset/base/347951 Log: MFC r347617: Add small EXAMPLE section to bsearch.3. Submitted by: fernape (via Phabricator) Reviewed by: bcr, jilles, dab Approved by: bcr (man pages), jilles (src) Differential Revision: https://reviews.freebsd.org/D19902 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 Sat May 18 02:02:14 2019 (r347950) +++ stable/12/lib/libc/stdlib/bsearch.3 Sat May 18 03:15:07 2019 (r347951) @@ -32,7 +32,7 @@ .\" @(#)bsearch.3 8.3 (Berkeley) 4/19/94 .\" $FreeBSD$ .\" -.Dd February 22, 2013 +.Dd May 15, 2019 .Dt BSEARCH 3 .Os .Sh NAME @@ -83,6 +83,61 @@ The function returns a pointer to a matching member of the array, or a null pointer if no match is found. If two members compare as equal, which member is matched is unspecified. +.Sh EXAMPLES +A sample program that searches people by age in a sorted array: +.Bd -literal +#include +#include +#include +#include +#include + +struct person { + char name[5]; + int age; +}; + +int +compare(const void *key, const void *array_member) +{ + int age = (intptr_t) key; + struct person person = *(struct person *) array_member; + + return (age - person.age); +} + +int +main() +{ + struct person *friend; + + /* Sorted array */ + struct person friends[6] = { + { "paul", 22 }, + { "anne", 25 }, + { "fred", 25 }, + { "mary", 27 }, + { "mark", 35 }, + { "bill", 50 } + }; + + size_t array_size = sizeof(friends) / sizeof(struct person); + + friend = bsearch((void *)22, &friends, array_size, sizeof(struct person), 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); + printf("name: %s\enage: %d\en", friend->name, friend->age); + + friend = bsearch((void *)30, &friends, array_size, sizeof(struct person), compare); + assert(friend == NULL); + printf("friend aged 30 not found\en"); + + return (EXIT_SUCCESS); +} +.Ed .Sh SEE ALSO .Xr db 3 , .Xr lsearch 3 ,