title: How to implement a [copy array to linked list] function?
body: <p><strong>What this supposed to do is:</strong></p>
  
  <ul>
  <li><p>Create an array with 4 elements.</p></li>
  <li><p>Print these 4 elements.</p></li>
  <li><p>Copy array elements to a created linked list in copy fucntion. </p></li>
  <li><p>Print linked list with print and traverse fuction.</p></li>
  </ul>
  
  <p><em>I tried this and it compiles, but crashes after printing the array.</em></p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;malloc.h&gt;
  #define ELEMENTS  4
  
  struct node {
      int data;
      struct node *next;
  };
  struct node *head;
  
  void insert(int x) {
      struct node *temp = malloc(sizeof(struct node));
      temp-&gt;data = x;
      temp-&gt;next = NULL;
  
      if (head != NULL)
          temp-&gt;next = head;
      head = temp;
  }
  
  void copy(struct node *head, int array[ELEMENTS], int n) {                       
      //copying array elements and create linked list
  
      struct node *temp = malloc(sizeof(struct node));
      temp-&gt;data = array[0];
      temp-&gt;next = NULL;
      head = temp;
  
      int i;
      for (i = 1; i &lt; n; i++) {
          struct node *temp2 = malloc(sizeof(struct node));
          temp-&gt;next = temp2;
          temp2-&gt;data = array[i];
          temp2-&gt;next = NULL;
          temp = temp2;
      }
  }
  
  void printlist() {
      struct node*temp = head;
      printf("List is : ");
  
      while (temp-&gt;next != NULL) {
        printf(" %d ", temp-&gt;data);
        temp = temp-&gt;next;
      }
      printf("\n");
  }
  
  int main() {
      int *array = (int*)calloc(ELEMENTS , sizeof(int));
      int i = 0;
      for (i = 0; i &lt; ELEMENTS; i++) {
          printf("arrays = [%d] ", i);
          scanf("%d", &amp;array[i]);
      }
  
      for (i = 0; i &lt; ELEMENTS; i++)
          printf("array [%d] = %d \n", i, array[i]);
  
          copy(head, array[ELEMENTS], ELEMENTS);
          printlist();
  
          getchar();
          return(0);
  }
  </code></pre>
  
  <p>How to fix it?</p>