I thought I got pointers but I don't fully understand a function that gets a new node for a linked list. I understand the theory behind Hash tables and chaining with linked lists but when it comes to implementing it in C, the syntax gets me confused.
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Hash table
node *table[N];
From what I understand, table is an array (of size N) that contains pointers to nodes, so each element in the array will eventually contain a memory address (e.g. 0x123) but does not contain the struct itself.
// Gets a node for a singly linked list
node *getNode(char *value)
{
node *pNode = malloc(sizeof(node));
if (pNode == NULL)
{
printf("Could not allocate memory for linked list node\n.");
return pNode;
}
strcpy(pNode->word, value);
// Set next to NULL pointer
pNode->next = NULL;
return pNode;
}
Consequently I don't understand pNode->next = NULL
as doesn't node *pNode
initialise the variable as a pointer to a node (which is just an address) and not the node struct. I would've thought it would be written as node pNode = malloc...
as the next
field within the struct would be a pointer to another node in the linked list.