title: Dynamic memory allocation - 2 dimensional array
body: <p>I have some troubles understanding pointers and dynamic memory allocation. I wrote those 2 codes: </p>
  
  <pre><code>    int **array;
      array = malloc(nrows * sizeof(int *));
      if(array == NULL)
      {
          fprintf(stderr, "out of memory\n");
          return -1;
      }
          for(i = 0; i &lt; nrows; i++)
      {
          *(array+i) = malloc(ncolumns * sizeof(int));
          if(array[i] == NULL)
          {
              fprintf(stderr, "out of memory\n");
              return -1;
          }
      }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>    int **array;
      array = malloc(nrows * sizeof(int *));
      if(array == NULL)
      {
          fprintf(stderr, "out of memory\n");
          return -1;
      }
          for(i = 0; i &lt; nrows; i++)
      {
          array[i] = malloc(ncolumns * sizeof(int));
          if(array[i] == NULL)
          {
              fprintf(stderr, "out of memory\n");
              return -1;
          }
      }
  </code></pre>
  
  <p>They should allocate space for 2 dimensional array. Although Im not sure if theyre both correct, I mean: does this line:</p>
  
  <p><code>array[i] = malloc(ncolumns * sizeof(int));</code></p>
  
  <p>do the exact same thing line this one:</p>
  
  <p><code>*(array+i) = malloc(ncolumns * sizeof(int));</code></p>
  
  <p>?</p>