HSEARCH
NAME SYNOPSIS DESCRIPTION RETURN VALUE CONFORMS TO BUGS EXAMPLE SEE ALSO
hcreate, hdestroy, hsearch - hash table management
#include item, ACTION action); int hcreate(unsigned nel); void hdestroy(void);
These three functions allow the user to create a hash table which associates a key with any data.
First the table must be created with the function hcreate(). nel is an estimate of the number of entries in the table. hcreate() may adjust this value upward to improve the performance of the resulting hash table. The GNU implementation of hsearch() will also enlarge the table if it gets nearly full. malloc(3) is used to allocate space for the table.
The corresponding function hdestroy() frees the memory occupied by the hash table so that a new table can be constructed.
item is of type ENTRY, which is a typedef defined in and includes these
{ char *key; char *data; } ENTRY; key points to the zero-terminated ASCII string which is the search key. data points to the data associated with that key. (A pointer to a type other than character should be cast to pointer-to-character.) hsearch() searches the hash table for an item with the same key as item, and if successful returns a pointer to it. action determines what hsearch() does after an unsuccessful search. A value of ENTER instructs it to insert the new item, while a value of FIND means to return NULL.
hcreate() returns NULL if the hash table cannot be successfully installed.
hsearch() returns NULL if action is ENTER and there is insufficient memory to expand the hash table, or action is FIND and item cannot be found in the hash table.
SVID, except that in SysV, the hash table cannot grow.
The implementation can manage only one hash table at a time. Individual hash table entries can be added, but not deleted.
The following program inserts 24 items in to a hash table, then prints some of them.
bsearch(3), lsearch(3), tsearch(3), malloc(3)
7 pages link to hsearch(3):