title: Create 2D array by passing pointer to function in c
body: <p>So I read dozens of examples of passing an 2D array pointer to function to get/change values of that array in function. But is it possible to create (allocate memory) inside the function. Something like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  void createArr(int** arrPtr, int x, int y);
  
  int main() {
  
      int x, y;       //Dimension
      int i, j;       //Loop indexes
      int** arr;      //2D array pointer
      arr = NULL;
      x=3;
      y=4;
  
      createArr(arr, x, y);
  
      for (i = 0; i &lt; x; ++i) {
          for (j = 0; j &lt; y; ++j) {
              printf("%d\n", arr[i][j]);
          }
          printf("\n");
      }
      _getch();    
  }
  
  void createArr(int** arrPtr, int x, int y) {
      int i, j;       //Loop indexes
      arrPtr = malloc(x*sizeof(int*));
      for (i = 0; i &lt; x; ++i)
          arrPtr[i] = malloc(y*sizeof(int));
  
      for (i = 0; i &lt; x; ++i) {
          for (j = 0; j &lt; y; ++j) {
              arrPtr[i][j] = i + j;
          }
      }    
  }
  </code></pre>