title: C - what is wrong with my mergesort on linked list?
body: <p>I got to the point where my program compiles (through vim, if that matters), but returns garbage. Here's the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  
  struct node {
      int *val;
      struct node *next;
  };
  
  struct node *addnode(int *val, struct node *next, int columns);
  struct node *mergesort(struct node *head, int column);
  struct node *merge(struct node *head_one, struct node *head_two, int column);
  
  int main(int argc, char *argv[])
  {
  
      struct node *head;
      struct node *current;
      struct node *next;
      int symbol = 0;
      int columns = 0;
      int rows = 0;
      head = NULL;
      int column = atoi(argv[1])-1;
      int lastSpace = 0;
  
      do {
          symbol = fgetc(stdin);
  
          if (rows == 0 &amp;&amp; (lastSpace == 0 &amp;&amp; (isspace(symbol) || feof(stdin)))) {
              columns++;
              lastSpace = 1;
          } else if (!isspace(symbol)) {
              lastSpace = 0;
          }
          if (symbol == '\n' || feof(stdin)) {
              rows++;
          };
      } while (symbol != EOF);
  
      if (ferror(stdin)) {
          printf("Error on reading from file.\n");
      } else {
          printf("The file contains %d row(s) and %d column(s).\n", rows, columns);
      };
  
      rewind(stdin); //ignore it - it works in this case
  
      int table[rows][columns], i, j;
      puts("Original file:\n");
      for (i = 0; i &lt; rows; i++) {
          head = addnode(*table, head, columns);
          for (j = 0; j &lt; columns; j++) {
              scanf("%d",&amp;table[i][j]);
              head-&gt;val[j] = table[i][j];
              printf("%d ",head-&gt;val[j]);
          };
          puts("\n");
      };
  
      /* Up to this point everything works */
      head = mergesort(head, column);
  
      printf("Sorted by %d column:\n", column+1);
      for(current = head; current != NULL; current = current-&gt;next) {
          for (j = 0; j &lt; columns; j++) {
              printf("%d ", current-&gt;val[j]); //suspect number 1 - printing loop prints only last node, instead node-by-node
          };//I removed a }; in this area
          puts("\n");
      };
  
      for(current = head; current != NULL; current = next)
          next = current-&gt;next, free(current);
      return 0;
  };
  
  
  struct node *addnode(int *val, struct node *next, int columns)
  {
      struct node *tnode;
      tnode = (struct node*)malloc(sizeof(*tnode));
      if(tnode != NULL) {
          tnode-&gt;val = malloc(sizeof(int) * columns); //suspect number 2 - I'm not sure I allocated memory correctly, it should allocate array of integers
          memcpy(tnode-&gt;val, val, sizeof(int) * columns);
          tnode-&gt;val = val;
          tnode-&gt;next = next;
      };
      return tnode;
  }
  
  struct node *mergesort(struct node *head, int column) //suspect number 3 - my modified mergesort duplicates last node
  {
      struct node *head_one;
      struct node *head_two;
      if((head == NULL) || (head-&gt;next == NULL))
          return head;
      head_one = head;
      head_two = head-&gt;next;
      while((head_two != NULL) &amp;&amp; (head_two-&gt;next != NULL)) {
          head = head-&gt;next;
          head_two = head-&gt;next-&gt;next;
      };
      head_two = head-&gt;next;
      head-&gt;next = NULL;
      return merge(mergesort(head_one, column), mergesort(head_two, column), column);
  }
  
  struct node *merge(struct node *head_one, struct node *head_two, int column) //suspect number 4 - instead of merging, it duplicates nodes
  {
      struct node *head_combined;
      if(head_one == NULL)
          return head_two;
      if(head_two == NULL)
          return head_one;
      if(head_one-&gt;val[column] &lt; head_two-&gt;val[column]) {
          head_combined = head_one;
          head_combined-&gt;next = merge(head_one-&gt;next, head_two, column);
      } else {
          head_combined = head_two;
          head_combined-&gt;next = merge(head_one, head_two-&gt;next, column);
      };
      return head_combined;
  }
  </code></pre>
  
  <p>I run it from a command line like so:<br>
      name_of_program column &lt; test.txt</p>
  
  <p>test.txt looks like this</p>
  
  <pre><code>1   4  100  34   12
  12  2  45   6     7
  5   8   1   87  101
  5   4  10   12   33
  </code></pre>
  
  <p>And instead of list sorted by <code>column</code> column, I get array from last node from test.txt 4 times. I have no idea how to analyse it step-by-step in vim, and I think that would help me pinpoint the problem.</p>