title: Passing a linked list head through a function as address in C
body: <p>I have a question regarding passing the head of a linked list in C through a function. So the code goes something like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  //Defining a structure of the node
  struct node { 
      int data;
      struct node* next;
      };
  
  void insert (struct node* rec, int x) {
      struct node* temp = (struct node*)malloc(sizeof(struct node));
      temp-&gt;data = x;
      temp-&gt;next = NULL;
      rec = temp; // head and rec is now pointing to the same node
  }
  
  void print(struct node* rec){
      printf("%d", rec-&gt;data); //error occurs here
      puts("");
  }
  
  main(){
      struct node *head = NULL; //head is currently pointing to NULL
      insert (head, 5); //Passing the head pointer and integer 5 to insert()
      print(head);
  }
  </code></pre>
  
  <p>So as you see, the error occurs when I tried printing rec->data. Why did the error occur? I thought since the pointer rec and head are all pointing to the same node in the heap, there should not be any problem? </p>
  
  <p>Thank you.</p>