| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- binary search a sorted table
#include <stdlib.h>void *bsearch(const void *key,const void *base,size_tnel,size_tsize,int (*compar)(const void *,const void *));
Thebsearch() function is a binary search routine generalized from Knuth (6.2.1)Algorithm B. It returns a pointer into a table (an array) indicatingwhere a datum may be found or a null pointer if thedatum cannot be found. The table must be previously sorted in increasingorder according to a comparison function pointed to bycompar.
Thekey argument points to a datum instance to be sought inthe table. Thebase argument points to the element at thebase of the table. Thenel argument is the number ofelements in the table. Thesize argument is the number ofbytes in each element.
The comparison function pointed to bycompar is called with two argumentsthat point to thekey object and to an array element, inthat order. The function must return an integer less than, equal to,or greater than 0 if thekey object is considered, respectively, tobe less than, equal to, or greater than the array element.
Thebsearch() function returns a pointer to a matching member of thearray, or a null pointer if no match is found. Iftwo or more members compare equal, which member is returned is unspecified.
The pointers to the key and the element at the base ofthe table should be of type pointer-to-element.
The comparison function need not compare every byte, so arbitrary data maybe contained in the elements in addition to the values being compared.
If the number of elements in the table is less than thesize reserved for the table,nel should be the lower number.
Thebsearch() function safely allows concurrent access by multiple threads to disjointdata, such as overlapping subtrees or tables.
Example 1 Examples for searching a table containing pointers to nodes.
The example below searches a table containing pointers to nodes consisting ofa string and its length. The table is ordered alphabetically on thestring in the node pointed to by each entry.
This program reads in strings and either finds the corresponding node andprints out the string and its length, or prints an error message.
#include <stdio.h>#include <stdlib.h>#include <string.h>struct node { /* these are stored in the table */ char *string; int length;};static struct node table[] = { /* table to be searched */ { "asparagus", 10 }, { "beans", 6 }, { "tomato", 7 }, { "watermelon", 11 },};main(){ struct node *node_ptr, node; /* routine to compare 2 nodes */ static int node_compare(const void *, const void *); char str_space[20]; /* space to read string into */ node.string = str_space; while (scanf("%20s", node.string) != EOF) { node_ptr = bsearch( &node, table, sizeof(table)/sizeof(struct node), 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: %20s\n", node.string); } } return(0);}/* routine to compare two nodes based on an *//* alphabetical ordering of the string field */static intnode_compare(const void *node1, const void *node2) { return (strcmp( ((const struct node *)node1)->string, ((const struct node *)node2)->string));}Seeattributes(5) for descriptions of the following attributes:
|
hsearch(3C),lsearch(3C),qsort(3C),tsearch(3C),attributes(5),standards(5)
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |