HP 3000 Manuals

hcreate [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Library Reference Manual

hcreate 

Allocates sufficient space for a hash table used by the hsearch function.

Syntax 

     #include <search.h>
     int hcreate (unsigned nel);

Parameters 

nel           An estimate of the maximum number of elements that the
              table contains.  This number may be adjusted upward by the
              algorithm to obtain a mathematically favorable table size.

Return Values 

!=0           Successful.  The space was allocated.

0             The space sufficient to contain the number of entries
              specified in nel was not available.  Therefore, no space
              was allocated.

Description 

The hcreate function allocates space sufficient for a hash table that is
to be searched by the hsearch function.  The size of the space is
determined by the nel parameter.  The hash table itself is an array of
pointers.  The size of the data elements to be searched is not relevant
to determining the amount of memory to be allocated for the hash table.

Only one hash search table may be active at any given time.

The hcreate function must be called before hsearch() to allocate
sufficient space for the hash table.


NOTE The hcreate function and the header file <search.h> are not part of ANSI C. Using them may make your program less portable.
The hdestroy function may be used to deallocate the hash table when it is no longer needed. 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> struct info { /* this is the info stored in the table */ int age, room; /* other than the key. */ }; #define NUM_EMPL 5000 /* number of elements in search table */ #define END_FLAG -1 /* sentinel value for age to terminate table input */ main( ) { /* space to store strings */ char string_space[NUM_EMPL*20]; /* space to store employee information */ struct info info_space[NUM_EMPL]; /* next available space in string_space */ char *str_ptr = string_space; /* next available space in info_space */ struct info *info_ptr = info_space; ENTRY item, *found_item, *hsearch( ); /* name to look for in table */ char name_to_find[30]; int i = 1; /* create table */ (void) printf("Enter name, age, and room for table. "); (void) printf("To terminate input, enter -1 for age.\n"); (void) hcreate(NUM_EMPL); do { if (scanf("%s%d%d", str_ptr,&info_ptr->age, &info_ptr->room) == EOF) exit(0); if (info_ptr->age == END_FLAG) break; /* put information into structure */ 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); } while (i++< NUM_EMPL); /* 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 */ found_item->key, ((struct info *)found_item->data)->age, ((struct info *)found_item->data)->room); } else { (void) printf("No such employee %\n", name_to_find); } } } See Also hsearch(), hdestroy()


MPE/iX 5.0 Documentation