HP-UX Reference (11i v2 04/09) - 3 Library Functions A-M (vol 6)
h
hsearch(3C) hsearch(3C)
/* put info in structure, and structure in item */
item.key = str_ptr;
item.data = (char *)info_ptr;
str_ptr += strlen(str_ptr) + 1;
info_ptr++;
/* put item into table */
(void) hsearch(item, ENTER);
}
/* access table */
item.key = name_to_find;
while (scanf("%s", item.key) != EOF) {
if ((found_item = hsearch(item, FIND)) != NULL) {
/* if item is in the table */
(void)printf("found %s, age = %d, room = %d\n",
found_item->key,
((struct info *)found_item->data)->age,
((struct info *)found_item->data)->room);
} else {
(void)printf("no such employee %s\n",
name_to_find);
}
}
}
RETURN VALUE
hsearch() returns a NULL pointer if either the action is FIND and the item could not be found or the
action is ENTER and the table is full.
hcreate() returns zero if it cannot allocate sufficient space for the table.
WARNINGS
hsearch() and hcreate() use malloc() to allocate space (see malloc(3C)).
Only one hash search table can be active at any given time.
SEE ALSO
bsearch(3C), lsearch(3C), malloc(3C), string(3C), tsearch(3C), thread_safety(5).
STANDARDS CONFORMANCE
hsearch(): AES, SVID2, SVID3, XPG2, XPG3, XPG4
hcreate(): AES, SVID2, SVID3, XPG2, XPG3, XPG4
hdestroy(): AES, SVID2, SVID3, XPG2, XPG3, XPG4
HP-UX 11i Version 2: September 2004 − 2 − Hewlett-Packard Company Section 3−−521