title: Dynamic memory allocation 2d array in C
body: <pre><code>int **twoDary = (int**) (malloc(rows * sizeof(int *)));
  int **twoDaryStart = twoDary;
  int *currentrow;
  
  for ( i = 0; i &lt; rows; i++ ){  // Originally: for (i = 0; i &lt; columns; i++)
      *(twoDary + i) =  (malloc(columns * sizeof(int)));
  }
  
  for (j = 0; j &lt; rows; j++) {
      currentrow = *(twoDary + j);
      for ( i = 0; i &lt; columns; i++ ) {
          *(currentrow + i) = i;
          printf("%d\n", *(currentrow+i));
      }
  }  
  </code></pre>
  
  <p>I'm trying to create a dynamic 2-D array. I am then trying to assign i ,the current i (in the inner for loop) to each element in every row. So my output should be the numbers 0 - column printed row times. </p>
  
  <p>I keep getting a seg fault if my rows and columns are not the same i.e. 5 rows 10 columns. Can anyone see from this code why that would be happening?</p>