Hi, I have a slight confusion about the function for updating head pointer of a linked list.
Here is the code:
Code:
struct node *deleteFirst(struct node *head)
{
if(head != NULL)
{
// store the old value of head pointer
struct node *temp = head;
// Change head pointer to point to next node
head = head->next;
// delete memory allocated for the previous head node
free(temp);
}
return head;
}
Here, I want to know, as head is a formal parameter of the function, it will be stored on the stack when the function is called and automatically destroyed when the function returns. So returning this head pointer from the function should result in some sort of error/ incorrect result instead of the head pointer being updated. Correct me where I am wrong. Waiting for the replies.