1<html lang="en"> 2<head> 3<title>bsearch - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Stdlib.html#Stdlib" title="Stdlib"> 9<link rel="prev" href="atoll.html#atoll" title="atoll"> 10<link rel="next" href="calloc.html#calloc" title="calloc"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<meta http-equiv="Content-Style-Type" content="text/css"> 13<style type="text/css"><!-- 14 pre.display { font-family:inherit } 15 pre.format { font-family:inherit } 16 pre.smalldisplay { font-family:inherit; font-size:smaller } 17 pre.smallformat { font-family:inherit; font-size:smaller } 18 pre.smallexample { font-size:smaller } 19 pre.smalllisp { font-size:smaller } 20 span.sc { font-variant:small-caps } 21 span.roman { font-family:serif; font-weight:normal; } 22 span.sansserif { font-family:sans-serif; font-weight:normal; } 23--></style> 24<link rel="stylesheet" type="text/css" href="../cs.css"> 25</head> 26<body> 27<div class="node"> 28<a name="bsearch"></a> 29<p> 30Next: <a rel="next" accesskey="n" href="calloc.html#calloc">calloc</a>, 31Previous: <a rel="previous" accesskey="p" href="atoll.html#atoll">atoll</a>, 32Up: <a rel="up" accesskey="u" href="Stdlib.html#Stdlib">Stdlib</a> 33<hr> 34</div> 35 36<h3 class="section">2.10 <code>bsearch</code>—binary search</h3> 37 38<p><a name="index-bsearch-16"></a><strong>Synopsis</strong> 39<pre class="example"> #include <stdlib.h> 40 void *bsearch(const void *<var>key</var>, const void *<var>base</var>, 41 size_t <var>nmemb</var>, size_t <var>size</var>, 42 int (*<var>compar</var>)(const void *, const void *)); 43 44</pre> 45 <p><strong>Description</strong><br> 46<code>bsearch</code> searches an array beginning at <var>base</var> for any element 47that matches <var>key</var>, using binary search. <var>nmemb</var> is the element 48count of the array; <var>size</var> is the size of each element. 49 50 <p>The array must be sorted in ascending order with respect to the 51comparison function <var>compar</var> (which you supply as the last argument of 52<code>bsearch</code>). 53 54 <p>You must define the comparison function <code>(*</code><var>compar</var><code>)</code> to have two 55arguments; its result must be negative if the first argument is 56less than the second, zero if the two arguments match, and 57positive if the first argument is greater than the second (where 58“less than” and “greater than” refer to whatever arbitrary 59ordering is appropriate). 60 61 <p><br> 62<strong>Returns</strong><br> 63Returns a pointer to an element of <var>array</var> that matches <var>key</var>. If 64more than one matching element is available, the result may point to 65any of them. 66 67 <p><br> 68<strong>Portability</strong><br> 69<code>bsearch</code> is ANSI. 70 71 <p>No supporting OS subroutines are required. 72 73 <p><br> 74 75 </body></html> 76 77