title: Why can't I print out this matrix/2D array?
body: <p>I am trying to type in a matrix and then print it, using functions. 
  The program crashes when it gets to the read_mat function. What am I getting wrong?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int** insert_mat(int **mat, int r, int c);
  
  
  int** read_mat(int** mat, int r, int c)
  {
      mat = (int**)calloc(r,sizeof(int*));
      for (int i=0; i&lt;r; i++)
      {
          mat[i]=(int*)calloc(c,sizeof(int));
      }
      insert_mat(mat, r, c);
      return mat;
  
  }
  
  
  int** insert_mat(int **mat, int r, int c)
  {
      for (int i=0; i&lt;r; i++)
      {
          for(int j=0; j&lt;c; j++)
          {
              printf("\nmat[%d][%d] = ",i , j);
              scanf("%d", &amp;mat[i][j]);
          }
      }
  
      return mat;
  }
  
  void print_mat(int **mat, int r, int c)
  {
      printf("\n");
  
      for (int i=0; i&lt;r; i++)
      {
          for(int j=0; j&lt;c; j++)
          {
              printf("%d ",mat[i][j]);
  
          }
          printf("\n");
      }
  
  }
  
  
  
  int main()
  {
      int **mat;
      int r1 =2;
      int c1 = 2;
      read_mat(mat, r1, c1);
      print_mat(mat, r1, c1);
  
      return 0;
  }
  </code></pre>
  
  <p>I also tried printing elements of mat in main (ex: printf("%d", mat[0][0]);) and it is also not working. So maybe the error is when I insert the elements? But I am not sure what I do wrong.</p>