Why when i have to declare a pointer to a node (head) I also have to allocate memory with malloc
or calloc
for it? I saw that the code that generate a list (not imported here) works well also without allocate memory for it and just declaring node *head
.
typedef struct str_node{
int data;
struct str_node *next;
}node;
int main(){
node *head;
head = (node*) malloc(sizeof(node));
head = NULL;
And why when I allocate memory like above i have to write (node*)
? Isn't it already allocated to a struct node since I'm doing it on head? What is the meaning of that line of code exactly? Moreover when I write head = NULL
am I set the address of the pointer head to NULL or what?