192 lines
6.8 KiB
HTML
192 lines
6.8 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>bsearch</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="bsearch"></a> <a name="tag_03_41"></a><!-- bsearch -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_03_41_01"></a>NAME</h4>
|
|
|
|
<blockquote>bsearch - binary search a sorted table</blockquote>
|
|
|
|
<h4><a name="tag_03_41_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<p><code><tt>#include <<a href="../basedefs/stdlib.h.html">stdlib.h</a>><br>
|
|
<br>
|
|
void *bsearch(const void *</tt><i>key</i><tt>, const void *</tt><i>base</i><tt>, size_t</tt> <i>nel</i><tt>,<br>
|
|
size_t</tt> <i>width</i><tt>, int (*</tt><i>compar</i><tt>)(const void *, const void *));<br>
|
|
</tt></code></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the
|
|
requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to
|
|
the ISO C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div>
|
|
|
|
<p>The <i>bsearch</i>() function shall search an array of <i>nel</i> objects, the initial element of which is pointed to by
|
|
<i>base</i>, for an element that matches the object pointed to by <i>key</i>. The size of each element in the array is specified by
|
|
<i>width</i>.</p>
|
|
|
|
<p>The comparison function pointed to by <i>compar</i> shall be called with two arguments that point to the <i>key</i> object and
|
|
to an array element, in that order.</p>
|
|
|
|
<p>The application shall ensure that the function returns an integer less than, equal to, or greater than 0 if the <i>key</i>
|
|
object is considered, respectively, to be less than, to match, or to be greater than the array element. The application shall
|
|
ensure that the array consists of all the elements that compare less than, all the elements that compare equal to, and all the
|
|
elements that compare greater than the <i>key</i> object, in that order.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_04"></a>RETURN VALUE</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>bsearch</i>() function shall return a pointer to a matching member of the array, or a null pointer if no match is found.
|
|
If two or more members compare equal, which member is returned is unspecified.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_05"></a>ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>No errors are defined.</p>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_03_41_06"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<p>The example below searches a table containing pointers to nodes consisting of a string and its length. The table is ordered
|
|
alphabetically on the string in the node pointed to by each entry.</p>
|
|
|
|
<p>The code fragment below reads in strings and either finds the corresponding node and prints out the string and its length, or
|
|
prints an error message.</p>
|
|
|
|
<pre>
|
|
<tt>#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
<br>
|
|
#define TABSIZE 1000
|
|
|
|
<br>
|
|
struct node { /* These are stored in the table. */
|
|
char *string;
|
|
int length;
|
|
};
|
|
struct node table[TABSIZE]; /* Table to be searched. */
|
|
.
|
|
.
|
|
.
|
|
{
|
|
struct node *node_ptr, node;
|
|
/* Routine to compare 2 nodes. */
|
|
int node_compare(const void *, const void *);
|
|
char str_space[20]; /* Space to read string into. */
|
|
.
|
|
.
|
|
.
|
|
node.string = str_space;
|
|
while (scanf("%s", node.string) != EOF) {
|
|
node_ptr = (struct node *)bsearch((void *)(&node),
|
|
(void *)table, TABSIZE,
|
|
sizeof(struct node), node_compare);
|
|
if (node_ptr != NULL) {
|
|
(void)printf("string = %20s, length = %d\n",
|
|
node_ptr->string, node_ptr->length);
|
|
} else {
|
|
(void)printf("not found: %s\n", node.string);
|
|
}
|
|
}
|
|
}
|
|
/*
|
|
This routine compares two nodes based on an
|
|
alphabetical ordering of the string field.
|
|
*/
|
|
int
|
|
node_compare(const void *node1, const void *node2)
|
|
{
|
|
return strcoll(((const struct node *)node1)->string,
|
|
((const struct node *)node2)->string);
|
|
}
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_07"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>The pointers to the key and the element at the base of the table should be of type pointer-to-element.</p>
|
|
|
|
<p>The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the
|
|
values being compared.</p>
|
|
|
|
<p>In practice, the array is usually sorted according to the comparison function.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_08"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_09"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_10"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="hcreate.html"><i>hcreate</i>()</a> , <a href="lsearch.html"><i>lsearch</i>()</a> , <a href=
|
|
"qsort.html"><i>qsort</i>()</a> , <a href="tsearch.html"><i>tsearch</i>()</a> , the Base Definitions volume of
|
|
IEEE Std 1003.1-2001, <a href="../basedefs/stdlib.h.html"><i><stdlib.h></i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_11"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_41_12"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>The DESCRIPTION is updated to avoid use of the term "must" for application requirements.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|