Reputation: 13
I would like to know how to declare, allocate and initialize array of Node to null.
typedef struct Nodes_Of_List {
int data;
struct Nodes_Of_List *next;
} Node;
//declare array of head nodes
Node *table;
//ALOCATE memory for "x" x is provided at runtime, number of head nodes
table = malloc(sizeof(Node) * x);
//Initialize elements to Null value
?? //How to do it
Explanation regarding initializing dynamic array of head nodes of linked list to Null requested. Purpose is to make array of linked lists, to make a hashtable.
Upvotes: 0
Views: 345
Reputation: 1883
Based on what I understood, you want to declare an array of head nodes. After that, you want to initialize them to NULL:
//declare array of head nodes statically
Node * table[x]; // x value provided at runtime
// Or dynamically
Node ** table = (Node **) malloc(sizeof(Node *) * x);
// Or use calloc directly wich will initialize your pointers to 0
Node ** table = (Node **) calloc(x, sizeof(Node *));
// table is an array of pointers. Each pointer references a linked list.
// Now you have just to put NULL value in each element of table
int i;
for(i = 0; i < x; i++)
table[i] = 0;
Upvotes: 1