title: Returning merged arrays from function in C
body: <p>I have the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  typedef struct _item {
      int v, w;
  } item;
  
  void printItems(item *t, int n) {
  int i;
      for(i= 0; i &lt; n; i++)
      printf("%4i",t[i].v);
      printf("\n");
  }
  
  item * newItemSet(int n) {
      int i;
      item *t = (item*)malloc(sizeof(item));
      for (i = 0; i &lt; n; i++){
          (t+i)-&gt;v = rand()%100;
          (t+i)-&gt;w = rand()%100;
      }
      return t;
  }
  
  item * mer(item*a, int n, item*b, int m) {
      int size = m+n;
      item*q = (item*)malloc(sizeof(item)*size);
      int i, c;
      c = 0;
      printf("a----\n");
      printItems(a,n);
      printf("b----\n");
      printItems(b,m);
      for(i = 0; i&lt;n; i++) {
          q[c] = a[i];
          c++;
      }
      for(i = 0; i&lt;m; i++) {
          q[c] = b[i];
          c++;
      }
      printItems(q,size);
      return q;
  }   
  
  
  int main(void) {
      srand(time(NULL));
      item * a = newItemSet(6);
      item *b = newItemSet(6);
      item *c = mer(a,6,b,6);
      printItems(c,12);   
      return 0;
  }
  </code></pre>
  
  <p>At every execution, the first part of <code>c</code> is correct, up to the last two elements of <code>b</code>. What did I miss? </p>