NAME | SYNOPSIS | API RESTRICTIONS | DESCRIPTION | EXAMPLES | NOTES | ATTRIBUTES | SEE ALSO | DIAGNOSTICS
$(NUCLEUS_DIR)/lib/classix/libsys.s.a #include <stdlib.h>void *bsearch(const void *key, const void *base, size_t nel, size_t size, int (*compar)const void *, const void *);
The function or functions documented here may not be used safely in all application contexts with all APIs provided in the ChorusOS 5.0 product.
See API(5FEA) for details.
The bsearch() function is a binary search routine generalized from Knuth (6.2.1) Algorithm B. It returns a pointer into a table indicating where an item of data may be found, or a null pointer if the item of data cannot be found. The table must be previously sorted in ascending order according to the comparison function indicated by compar.The key value points to the item of data to search for. The base pointer indicates the element at the base of the table, nel is the number of elements in the table, and size is the number of bytes in each element. The function pointed to by compar is called with two arguments that point to the elements being compared. The function must return an integer less than, equal to, or greater than zero, depending on whether the first argument is to be considered less than, equal to, or greater than the second.
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.
This code fragment reads in strings and either finds the corresponding node and prints out the string and its length, or prints an error message.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define TABSIZE 1000 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(&node, 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 strcmp( ((const struct node *)node1)->string, ((const struct note *)node2)->string); }
The pointers to the key and the element at the base of the table should be of the type pointer-to-element. The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. If the number of elements in the table is less than the size reserved for the table, nel should be the lower number.
See attributes(5) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
---|---|
Interface Stability | Evolving |
A NULL pointer is returned if the key cannot be found in the table.
NAME | SYNOPSIS | API RESTRICTIONS | DESCRIPTION | EXAMPLES | NOTES | ATTRIBUTES | SEE ALSO | DIAGNOSTICS