With this function signature:
void changeNode(struct node *head)
You have a pointer to a node, and you can change this structure. You cannot change what the variable head points to. Suppose the following definition of a struct node :
struct node { int field1; struct node *next; }
Given a function signature and a struct node consider the following operations that can change the structure in a function:
void changeNode(struct node *head) { head->field1 = 7; head->next = malloc(sizeof(struct node)); }
C is the value passed: when we pass a variable to a function, the function receives a copy. This is why we pass a pointer to a struct node so that we can change it, and the consequences of these changes are outside the function. But we still get only a copy of the pointer itself. Thus, the following operation is not useful:
void changeNode(struct node *head) {
Changes to head will not be displayed outside the function. To change what head indicates, we must use an additional level of indirection:
void changeNode(struct node **head) {
Now changes to the head are reflected outside the function.
source share