1

i have the following bucket entry structure and hash table set up

typedef struct Hash_Entry
{
    struct Hash_Entry *next;    
    void        *key_Data;  
    unsigned    key_hash;   
    char        key[5]; 
} Hash_Entry;

typedef struct Hash_Table 
{
    struct Hash_Entry **bucketPtr;  /* Buckets in the table */
    int         size;       /* Actual size of array. */
    int         numEntries; /* Number of entries in the table. */
    int         mask;       /* Used to select bits for hashing. */
} Hash_Table;

I want to create an array(or a dynamic array) of this Hash_Table so that when I feel the table is full I can create another table instead of re sizing it

2 Answers 2

1

Something like:

void hash_table_init(Hash_Table *table, size_t entries)
{
  size_t i;

  table->size = 0;
  table->numEntries = entries;
  table->bucketPtr = malloc(table->numEntries * sizeof *table->bucketPtr);
  for(i = 0; i < table->numEntries; i++)
    table->bucketPtr[i] = NULL;
  table->mask = 0; /* Not sure how to initialize this. */
}

I don't quite see the point of leaving the initial buckets as pointers, I'd probably just do

typedef struct {
  ...
  Hash_Entry *buckets;
  ...
} Hash_Table;

Assuming that most buckets will actually be used, so why not have them. :)

Sign up to request clarification or add additional context in comments.

Comments

0

you can create an array using malloc from stdlib

Hash_Table* array = (Hash_Table*)malloc(sizeof(Hash_Table) * 100);

and when the array is full you can do a realloc.

you can have a look at:

Create dynamic sized array of user defined structure

1 Comment

He said he doesn't want to resize that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.