title: Pointer return by a function in C
body: <p>Following is the code for making a linked list which has 2 pointers. Linked list is getting created (printed) and all pointers (prev + next) are fine. But when I call the function "copay" and assign its value(pointer) to "duplicate", I am getting segmentation fault but if I use only "copay" and don't assign it to any other variable then there is no issue.   </p>
  
  <pre><code>typedef struct node {
      int data;
      struct node *next;
      struct node *prev;
  } node;
  
  
  void insert(node **head, int data) {
      node *new = (struct node *)malloc(sizeof(node));
      new-&gt;data = data;
      new-&gt;next = NULL;
      node *temp = *head;
      if (!(temp)) {
          *head = new;
          new-&gt;prev = NULL;
          // printf("\n return  : %d",data);
          return;
      }
  
      while (temp-&gt;next)
          temp = temp-&gt;next;
  
      temp-&gt;next = new;
      new-&gt;prev = temp;
      // printf("\n return  : %d",data);
  }
  
  void print(node **head) {
      node *temp = *head;
      printf("\n");
      while (temp) {
          printf(" %d -&gt;", temp-&gt;data);
          temp = temp-&gt;next;
      }
      printf(" NULL\n");
  }
  
  node *copay(node **head) {
      node *temp = *head;
      return temp;
  }
  
  int main() {
      node *head;
  
      insert(&amp;head, 1);
      insert(&amp;head, 3);
      insert(&amp;head, 5);
      insert(&amp;head, 7);
      insert(&amp;head, 9);
      (head)-&gt;prev = (head)-&gt;next-&gt;next;
      (head)-&gt;next-&gt;next-&gt;prev = (head)-&gt;next-&gt;next-&gt;next-&gt;next;
      (head)-&gt;next-&gt;next-&gt;next-&gt;next-&gt;prev = (head)-&gt;next;
  
  
      print(&amp;head);
      node *duplicate = copay(&amp;head);
  
      // print(&amp;duplicate);
  }
  </code></pre>