HP-UX Reference (11i v2 07/12) - 3 Library Functions A-M (vol 6)
b
bsearch(3C) bsearch(3C)
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)
struct node *node1, *node2;
{
return strcoll(((const struct node *)node1)->string,
((const struct node *)node2)->string);
}
WARNINGS
If the table being searched contains two or more entries that match the selection criteria, a random entry is
returned by bsearch() as determined by the search algorithm.
SEE ALSO
hsearch(3C), lsearch(3C), qsort(3C), tsearch(3C), thread_safety(5).
STANDARDS CONFORMANCE
bsearch(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C
HP-UX 11i Version 2: December 2007 Update − 2 − Hewlett-Packard Company 141