Just trying to make a kind of hash table with each node being a linked list.
Having trouble just initializing the space, what am I doing wrong?
#include <stdlib.h>
typedef struct entry {
struct entry *next;
void *theData;
} Entry;
typedef struct HashTable {
Entry **table;
int size;
} HashTable;
int main(){
HashTable *ml;
ml = initialize();
return 0;
}
HashTable *initialize(void)
{
HashTable *p;
Entry **b;
int i;
if ((p = (HashTable *)malloc(sizeof(HashTable *))) == NULL)
return NULL;
p->size = 101;
if ((b = (Entry **)malloc(p->size * sizeof(Entry **))) == NULL)
return NULL;
p->table = b;
for(i = 0; i < p->size; i++) {
Entry * b = p->table[i];
b->theData = NULL;
b->next = NULL;
}
return p;
}
p = (HashTable *)malloc(sizeof(HashTable *)), but ratherp = malloc(sizeof(*p)). The cast is only useful in order to ensure thatphas the "right type", i.e. the same type as you used for the size. In which case the cast should be to a type with one more asterisk than the type used insizeof. Since you've got it wrong here anyway, the cast is doing you no good at all, and can obscure an error message that results from forgetting to include stdlib.h.