Passing a pointer is basically like passing a pointer as value.. the changes to the pointer internally in the function will not modify the actual value of the pointer.. but when we need to access on the actual pointer itself in a function, then we are coming up with the pointer to pointer concept. this is my understanding..
struct node
{
int data;
struct node* next;
};
void push(struct node** head_ref, int new_data) // i understand the need of pointer to pointer here, since we are changing the actual value by adding a node..
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void insertAfter(struct node* prev_node, int new_data) // why are we not using pointer to pointer here since even here the pointer data is getting modified..??
{
if (prev_node == NULL)
{
return;
}
struct node* new_node =(struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
int main()
{
struct node* head = NULL;
append(&head, 6);
insertAfter(head->next, 8);
return 0;
}
Please clarify.. i'm confused why we are not using pointer to pointer in InsertAfter(...) as well thought we change the pointer there?