# hcreate

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           hcreate, hdestroy, hsearch — manage hash search table
    


## SYNOPSIS



           #include <search.h>
    
           int hcreate(size_t _nel_);
           void hdestroy(void);
           ENTRY *hsearch(ENTRY _item_ , ACTION _action_);
    


## DESCRIPTION



           The _hcreate_(), _hdestroy_(), and _hsearch_() functions shall manage
           hash search tables.
    
           The _hcreate_() function shall allocate sufficient space for the
           table, and the application shall ensure it is called before
           _hsearch_() is used. The _nel_ argument is an estimate of the maximum
           number of entries that the table shall contain. This number may be
           adjusted upward by the algorithm in order to obtain certain
           mathematically favorable circumstances.
    
           The _hdestroy_() function shall dispose of 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.
    
           The _hsearch_() function is a hash-table search routine. It shall
           return a pointer into a hash table indicating the location at
           which an entry can be found. The _item_ argument is a structure of
           type **ENTRY**(defined in the _< search.h>_ header) containing two
           pointers: _item.key_ points to the comparison key (a **char ***), and
           _item.data_ (a **void ***) points to any other data to be associated
           with that key. The comparison function used by _hsearch_() is
           _strcmp_().  The _action_ argument is a member of an enumeration type
           **ACTION** indicating the disposition of the entry if it cannot be
           found in the table. ENTER indicates that the item should be
           inserted in the table at an appropriate point. FIND indicates that
           no entry should be made.  Unsuccessful resolution is indicated by
           the return of a null pointer.
    
           These functions need not be thread-safe.
    


## RETURN VALUE



           The _hcreate_() function shall return 0 if it cannot allocate
           sufficient space for the table; otherwise, it shall return non-
           zero.
    
           The _hdestroy_() function shall not return a value.
    
           The _hsearch_() function shall return 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.
    


## ERRORS



           The _hcreate_() and _hsearch_() functions may fail if:
    
           **ENOMEM** Insufficient storage space is available.
    
           _The following sections are informative._
    


## EXAMPLES



           The following example reads in strings followed by two numbers and
           stores them in a hash table, discarding duplicates. It then reads
           in strings and finds the matching entry in the hash table and
           prints it out.
    
               #include <stdio.h>
               #include <search.h>
               #include <string.h>
    
               struct info {        /* This is the info stored in the table */
                   int age, room;   /* other than the key. */
               };
    
               #define NUM_EMPL    5000    /* # of elements in search table. */
    
               int main(void)
               {
                   char string_space[NUM_EMPL*20];   /* Space to store strings. */
                   struct info info_space[NUM_EMPL]; /* Space to store employee info. */
                   char *str_ptr = string_space;     /* Next space in string_space. */
                   struct info *info_ptr = info_space;
                                                     /* Next space in info_space. */
                   ENTRY item;
                   ENTRY *found_item; /* Name to look for in table. */
                   char name_to_find[30];
    
                   int i = 0;
    
                   /* Create table; no error checking is performed. */
                   (void) hcreate(NUM_EMPL);
                   while (scanf("%s%d%d", str_ptr, &info_ptr->age,
                          &info_ptr->room) != EOF && i++ < NUM_EMPL) {
    
                       /* Put information in structure, and structure in item. */
                       item.key = str_ptr;
                       item.data = 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 0;
               }
    


## APPLICATION USAGE



           The _hcreate_() and _hsearch_() functions may use _malloc_() to allocate
           space.
    


## RATIONALE



           None.
    


## FUTURE DIRECTIONS



           None.
    


* * *