title: Splitting a 2D array to an array of smaller 2D arrays in C
body: <p>Given:</p>
  
  <pre><code>1 2 3 4 5 6 7 8
  1 2 3 4 5 6 7 8 
  1 2 3 4 5 6 7 8
  </code></pre>
  
  <p>I want to split the 2d array (struct MATRIX) into the an array of struct MATRIX
  given a chunksize CS:
  assume cs to be 2, 
  the answer would be</p>
  
  <pre><code>Seg[0]:
  1 2 
  1 2 
  1 2
  Seg[1]:
  3 4 
  3 4 
  3 4
  ....
  Seg[3]:
  7 8
  7 8
  7 8
  </code></pre>
  
  <p>Here is my Matrix Struct:</p>
  
  <pre><code>typedef struct MATRIX {
      int nrow;
      int ncol;
      int **element;
  } MATRIX;
  </code></pre>
  
  <p>and here is the function the seperates them:</p>
  
  <pre><code>void SegmentMatrix(MATRIX input,MATRIX* segs,int Chunksize, int p) {
      int i,j,r;
  
      //Allocate segs
      for (i = 0; i&lt;p;i++)
      {
          CreateMatrix(&amp;(segs[i]),input.nrow ,Chunksize,0);
      }
  
      //Now Copy the elements from input to the segs
      //where seg0 takes from 0 to cs cols of a, and all their rows, and seg1 takes from cs to 2cs ...
      printf("Stats:\n\t P: %d\t CS: %d\n",p,Chunksize);
      for (r = 0; r&lt;p; r++) {
          for (i = 0; i&lt;input.nrow;i++) {
              for (j = r*Chunksize; j&lt;r*Chunksize+Chunksize-1; j++) {
                   //I tried (&amp;(segs[r]))-&gt;element... Doesn't work, produces wrong data
                   segs[r].element[i][j] = input.element[i][j];
  
          }
      }
      PRINTM(segs[r]);
      }
  
  
  }
  </code></pre>
  
  <p>Note that PRINTM basically prints the matrix, it knows the limits by checking segs[r].nrow and ncol
  and CreateMatrix takes the following inputs (&amp;matrix, number of rows, number of colums, filltype) and mallocs from within.</p>
  
  <pre><code>filltype: 
  0- generates zeroth matrix
  1- generates identity
  else A[i][j] = j; for simplicity
  </code></pre>
  
  <p>The problem is that the if i print the matrices Segs[i], they all come down with their default value given by CreateMatrix, and not the newly added values.</p>
  
  <p>CLARIFICATION:
  Okay, so if you guys check that last PRINTM in SegmentMatrix function, it outputs the matrices as if the for loops didn't happen, aka, i can delete the for loops and would get the same output.. 
  did i do something wrong in this line (taken from the SegmentMatrix)</p>
  
  <pre><code>Segs[r].element[i][j] = input.element[i][j];
  </code></pre>