title: filling an array in a loop
body: <p>I have a function that constantly gets called. I have 3 smaller arrays that I need to pack into a larger array each loop</p>
  
  <pre><code>float* a;
  float* b;
  float* c;
  float* abc;
  
  a = calloc(1, sizeof(float)*3);
  b = calloc(1, sizeof(float)*3);
  c = calloc(1, sizeof(float)*3);
  abc = calloc(1, sizeof(float)*9);
  
  
  void update() {
  
     for(int i = 0; i &lt; 3; i++) {
       fill_a(a);
       fill_b(b)
       fill_c(c);
  
       abc[0] = a[0];
       abc[1] = b[0];
       abc[2] = c[0];
  
       abc[0] = a[1];
       abc[1] = b[1];
       abc[2] = c[1];
  
       abc[0] = a[2];
       abc[1] = b[2];
       abc[2] = c[2];
     }
  }
  
  free(a);
  free(b);
  free(c);
  free(abc);
  </code></pre>
  
  <p>the implementation above has problems because the values get overwritten on subsequent loops.</p>
  
  <p>I've tried adding an offset to the values like this:</p>
  
  <pre><code>   for(int i = 0; i &lt; 3; i++) {
       fill_a(a);
       fill_b(b)
       fill_c(c);
  
       abc[(i*9)+0] = a[0];
       abc[(i*9)+1] = b[0];
       abc[(i*9)+2] = c[0];
  </code></pre>
  
  <p>and while that seem to work, if I try to add one by one that also doesn't work.</p>
  
  <p>I've also tried adding offsets via an index value but it counts off to infinity.</p>
  
  <pre><code>int idx = 0;
  
  void update() {
  
     for(int i = 0; i &lt; 3; i++) {
       fill_a(a);
       fill_b(b)
       fill_c(c);
  
       abc[++idx] = a[0];
       abc[++idx] = b[0];
       abc[++idx] = c[0];
  
       abc[++idx] = a[1];
       abc[++idx] = b[1];
       abc[++idx] = c[1];
  </code></pre>
  
  <p>i've also tried filling the first arrays in one for loop. Then later in the update loop putting those values into the larger array.</p>
  
  <pre><code>int idx;
  
  idx = 0;
  void update() {
  
     for(int i = 0; i &lt; 3; i++) {
       fill_a(a);
       fill_b(b)
       fill_c(c);
     }
    int tidx = idx;
    abc[++tidx] a[0];
    abc[++tidx] b[0];
    abc[++tidx] c[0];
    ....
    idx = tidx;
   }
  </code></pre>
  
  <p>but again the idx runs off to infinity. How can I arrange this algorithm so that I can fill the larger array inside that loop with the values from the smaller array while also keeping the offsets in order?</p>
  
  <p>The values of the larger array is always going to be overwritten ever loop so after it's filled, it's consumed elsewhere then overwritten the next call to update.</p>
  
  <p><strong>edit</strong>
  I have tried gdb debugging stepping through the code one variable at a time.</p>
  
  <p>The expected output is something like this:</p>
  
  <pre><code>a = { 0, 1, 2 };
  b = { -3, -2, -1 };
  
  abc = { 0, -3, 11.
          1, -2, 22 };
  </code></pre>
  
  <p>Here's a fully working example from actual code.</p>
  
  <pre><code>    int count = 3;
      float* v_buff;
      float* c_buff;
  
      size_t vs = sizeof(float) * 6);
      v_buff = calloc(1, (vs));
  
  
  void update() {
       for(int i = 0; i &lt; count; i++) {
           float a[3] = { 0, 1, 2}; //these will be new every update
           float b[3] = { -3, -2, -1};
           int idx = 0;
           v_buff[++idx] = a[0];
           v_buff[++idx] = a[1];
           v_buff[++idx] = a[2];
  
           v_buff[++idx] = b[0];
           v_buff[++idx] = b[1];
           v_buff[++idx] = b[2];
       }
  }
  </code></pre>
  
  <p>That sample code compiles but the same thing applies.
  trying the offset:</p>
  
  <pre><code>v_buff[(i * 3) + (++idx)] = a[0];
  </code></pre>
  
  <p>causes my counter to run off to infinity or just overwrites the first set of values in v_buff.</p>