Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

hsearch, hcreate, hdestroy - manage hash tables

&pagelevel(4)&pagelevel

Syntax

#include <search.h>

ENTRY *hsearch(ENTRY item, ACTION action);
int hcreate(size_t nel);
void hdestroy(void); 

Description

hsearch() is a hash-table search routine. It returns a pointer into a hash table indicating
the location at which an entry can be found. The comparison function used by hsearch() is
strcmp(). item is a structure of type ENTRY (defined in the search.h header) containing two
pointers: item.key points to the comparison key (of type char*), and item.data (a void*)
points to any other data to be associated with that key.

Extension
Pointers to types that are not void must be converted to pointers to void. (End)

action is a member of the enumeration type ACTION (defined in search.h) indicating the
disposition of the entry if it cannot be found in the table. ENTER indicates that item should be
inserted in the table at an appropriate point.

Extension
If a duplicate to an existing entry is present, the new item is not entered, and hsearch()
returns the pointer to the existing entry. (End)

FIND indicates that no entry should be made. Unsuccessful resolution is indicated by the
return of a null pointer.

hcreate() allocates sufficient space for the table and must be called before hsearch() is
used. The nel argument is an estimate of the maximum number of entries that the table will
contain. This number may be adjusted upward by the algorithm in order to obtain certain
mathematically favorable circumstances.

hdestroy() destroys the search table and may be followed by another call to hcreate().
After the call to hdestroy(), the data can no longer be considered accessible.

Return val.

hsearch() :
Pointer to the found entry



if successful.


Null pointer

if the action is FIND and the item could not be found, or if the
action is ENTER and the table is full.


hcreate() :  


Null pointer

hcreate(): if it cannot allocate sufficient space for the table.


hdestroy()  does not return a value.

Errors

hsearch() will fail if:

ENOMEM      Insufficient storage space is available.

Notes

hsearch() and hcreate() use malloc() to allocate space.

Extension
Only one hash search table may be active at a given time. (End) 

See also

bsearch(), lsearch(), malloc(), strcmp(), tsearch(), search.h.