title: How to make an array of linked lists, whose list points to a matrix row in C?
body: <p>I have a type:</p>
  <pre><code>typedef struct node
  {
      double data;
      struct node * next;
  }NODE;
  </code></pre>
  <p>which represents a linked list node.</p>
  <p>I have a function:</p>
  <pre><code>void add(NODE ** head, double data)
  {
      NODE * new = NULL;
      new = malloc(sizeof(NODE));
      new-&gt;next = NULL;
      new-&gt;data = data;
      if(* head == NULL) * head = new;
      else
      {
          NODE * temp = NULL;
          for(temp = * head; temp-&gt;next; temp = temp-&gt;next);
          temp-&gt;next = new;
      }
  }
  </code></pre>
  <p>which adds a new number at the end of the list.</p>
  <p>I need to write a function (using the function 'add') to form and return a dynamically allocated array of 'n' linked lists (every element of the array points to the first node of the list) based on a matrix of dimensions 'nxm', where the elements of the k-th list should correspond to the elements of the k-th order of the matrix.</p>
  <p>Function prototype:</p>
  <pre><code>NODE ** mat2arr(double ** matrix, int n, int m)
  </code></pre>
  <p>where 'matrix' represents array of pointers, n number of rows and m number of columns.</p>
  <p>I tried it like this but it gives me segmentation errors. How can i fix the code or rewrite it with new ideas, also how could i access the returned array of linked lists so i can print the values ?</p>
  <pre><code>NODE ** mat2arr(double ** matrix, int n, int m)
  {
      NODE ** array_list = NULL;
      array_list = malloc(n * sizeof(NODE *));
      for(int i = 0; i &lt; n; i++)
      {
          NODE * list = NULL;
          for(int j = 0; j &lt; m; j++)
              add(&amp;list, matrix[i][j]);
          array_list[i] = list;
      }
      return array_list;
  }
  </code></pre>
  <p>Calling the function mat2arr:</p>
  <pre><code>NODE ** array_list = NULL;
  array_list = mat2arr(matrix, n, m);
  //matrix represents a 2D array, a matrix
  </code></pre>