title: Inserting a node at the end of a doubly linked list
body: <p>I tried inserting node at the end of doubly-linked list with a insert function but the process execution stops abruptly after second insertion. I tried checking out address of all pointer variable used in insert function and they show that my code works fine until the third insert function. How can I fix my code?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct node {
      int data;
      struct node* next;
      struct node* prev;
  };
  
  void insert(struct node** header,int data) {
      struct node* newnode=(struct node*)malloc(sizeof(struct node*));
      newnode-&gt;data=data;
      newnode-&gt;prev=NULL;
      newnode-&gt;next=NULL;
  
      if(*header == NULL) {
          *header=newnode;
          return;
      }
  
      struct node* temp = *header;
  
      while(temp-&gt;next != NULL) {
          temp=temp-&gt;next;
      }
  
      temp-&gt;next = newnode;
      newnode-&gt;prev = temp;
      printf("%d\n**\n", temp-&gt;next);
  }
  
  void main() {
      struct node* head = NULL;
      insert(&amp;head, 4);
      insert(&amp;head, 45);
      printf("\n main head %d", head);
      insert(&amp;head, 8);
      printf("testing");
      insert(&amp;head, 69);
  }
  </code></pre>