The struct that will represent the node of the linked list:
typedef struct node{
int val;
struct node *next;
} node_t;
and the head of our list:
node_t *head;
Now, I wanna build a function that creates the first element in the list, of course that will be pointed by *head. I am gonna start with the correct version, in my point of view, of the function, where I used a pointer who points to head, namely a double pointer:
void createFirstElement(node_t **head, int value){
*head=NULL;
*head=malloc(sizeof(node_t));
(*head)->val=value;
(*head)->next=NULL;
}
When I used that version of createFirstElement I got the value of the node printed. However I have a question for my first version of createFirstElement which didn't worked:
void createFirstElement(node_t *head, int value){
head=NULL;
head=malloc(sizeof(node_t));
head->val=value;
head->next=NULL;
}
How is this version different from the one with double pointers? I am still getting the head pointer in parameter (instead of a pointer that points to head) and make all the changes inside.
Thanks everyone in advance!