I am quite confused about malloc in struct. I create a linked list with both head and tail. Each node have a pointer point to a string, and a pointer point to the next node:
struct node {
char* string;
struct node_t* next;
}
struct linkedlist {
struct node* head;
struct node* tail;
};
When initialise them, do I need to malloc all of list, node and string at first? Or only need to malloc some of them? And what would be the difference if I only set them to NULL without malloc? And where would be an appropriate place to malloc them? For example, at make_linkedlist function, do I also malloc head and tail directly after malloc list? Or I can set them to NULL first, then malloc at create_node function? What about string? Do I need to also malloc space for it? If I would only assign the string pointer to another string pointer, do I write:
string = (char*)malloc(sizeof(char) * size);
or
*string = "";
or
*string = NULL:
?
Thanks a lot if anybody can help!