typedef struct kvNode
{
int key;
int value;
struct kvNode* next;
} kvNode_t;
typedef struct
{
int size;
kvNode_t** arrayOfKvNodes;
} hashTable_t;
bool hashTable_create(hashTable_t *ht, int size)
{
int i;
ht = (hashTable_t *)malloc(sizeof(hashTable_t));
if(ht == NULL)
{
return false;
}
ht->size = size;
ht->arrayOfKvNodes = (kvNode_t **)malloc(size * sizeof(kvNode_t *));
if(ht->arrayOfKvNodes == NULL)
{
return false;
}
// initialize every cell to NULL
for(i=0; i<size; i++)
{
ht->arrayOfKvNodes[i] = NULL;
}
return true;
}
void main()
{
hashTable_t hashTableA;
printf("Create Hash Table\n");
hashTable_create(&hashTableA, 10);
printf("Size of hash table: %d\n", hashTableA.size);
}
I'm trying to implement a hash table in C
. I have a create function here that initializes an array of key-value nodes list **arrayOfKvNodes
.
The above code results in an unexpected behavior.
Create Hash Table
Size = -2039534832
However, when I try this function prototype: hashTable_t *hashTable_create(int size)
and return a pointer to the malloced hashTable_t *
, the code works fine.
What's the difference between these two?