xref: /original-bsd/lib/libc/stdlib/bsearch.3 (revision e58c8952)
1.\" Copyright (c) 1990, 1991, 1993, 1994
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" %sccs.include.redist.man%
9.\"
10.\"     @(#)bsearch.3	8.3 (Berkeley) 04/19/94
11.\"
12.Dd
13.Dt BSEARCH 3
14.Os
15.Sh NAME
16.Nm bsearch
17.Nd binary search of a sorted table
18.Sh SYNOPSIS
19.Fd #include <stdlib.h>
20.Ft void *
21.Fn bsearch "const void *key" "const void *base" "size_t nmemb" "size_t size" "int (*compar) (const void *, const void *)"
22.Sh DESCRIPTION
23The
24.Fn bsearch
25function searches an array of
26.Fa nmemb
27objects, the initial member of which is
28pointed to by
29.Fa base ,
30for a member that matches the object pointed to by
31.Fa key .
32The size of each member of the array is specified by
33.Fa size .
34.Pp
35The contents of the array should be in ascending sorted order according
36to the comparison function referenced by
37.Fa compar .
38The
39.Fa compar
40routine
41is expected to have
42two arguments which point to the
43.Fa key
44object and to an array member, in that order, and should return an integer
45less than, equal to, or greater than zero if the
46.Fa key
47object is found, respectively, to be less than, to match, or be
48greater than the array member.
49.Sh RETURN VALUES
50The
51.Fn bsearch
52function returns a pointer to a matching member of the array, or a null
53pointer if no match is found.
54If two members compare as equal, which member is matched is unspecified.
55.Sh SEE ALSO
56.Xr db 3 ,
57.Xr lsearch 3 ,
58.Xr qsort 3 ,
59.\" .Xr tsearch 3
60.Sh STANDARDS
61The
62.Fn bsearch
63function conforms to
64.St -ansiC .
65