title: How to properly pass dynamic 2-d array into a function in C
body: <p>I need to dynamically allocate a 2-d array in a function with <code>n</code> rows and <code>m</code> columns that are also has to be read from <code>input</code> function.</p>
  <pre><code>int main()
  {
      int** matrix;
      int n, m;
      input(matrix, &amp;n, &amp;m);
      return 0;
  } 
  
  void input(int** matrix, int* n, int* m) {
      if (scanf(&quot;%d&quot;, n) == 1 &amp;&amp; getchar() == '\n') {
          if (*n &lt;= 0) {
              error = 1;
          }
      }
      if (error == 0) {
          if (scanf(&quot;%d&quot;, m) == 1 &amp;&amp; getchar() == '\n') {
              if (*m &lt;= 0) {
                  error = 1;
              }
          }
      }
      if (error == 0) {
          matrix = (int**)calloc(*n, sizeof(int*));
          for (int i = 0; i &lt; *n; i++) {
              matrix[i] = (int*)calloc(*m, sizeof(int));
          }
          for (int i = 0; i &lt; *n &amp;&amp; error == 0; i++) {
              for (int j = 0; j &lt; *m &amp;&amp; error == 0; j++) {
                  if (scanf(&quot;%d&quot;, &amp;matrix[i][j]) == 1) {
                  }
                  else {
                      error = 1;
                  } 
              }
          }
          for (int i = 0; i &lt; *n; i++) {
              for (int j = 0; j &lt; *m; j++) {
                  printf(&quot;%d &quot;, matrix[i][j]);
              }
              printf(&quot;\n&quot;);
          }
      }
  }
  </code></pre>
  <p>It scans and prints values properly, but if I try to interact with <code>matrix</code> outside this function or in another function (e.g. <code>output(int** matrix, int *n, int *m</code> that just prints all the values) I'll get memory error.
  How do i suppose to fix that? (I need to keep double pointer).</p>