title: C - Read matrix of unknown size from file
body: <p>I have file that has 30 matrices and each matrix has unknown size of rows and columns(with a max size of 1000). For instance:</p>
  
  <pre><code>0 5 2
  5 0 2
  1 6 0
  
  0 9 7 4
  3 0 9 1
  9 1 0 4
  9 4 1 0
  </code></pre>
  
  <p>I need to read each matrix into a 2d array. What would be the most efficient way of doing this? </p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>int** mat=malloc(1000000*sizeof(int*)); 
  for(i=0;i&lt;1000000;++i)
      mat[i]=malloc(4*sizeof(int));
  
  while(!feof(file))
      {
          for(i=0;i&lt;1000;i++)
          {
              for(j=0;j&lt;1000;j++){
                  fscanf(file,"%d%*[^\n]%*c",&amp;mat[i][j]);
                  printf("%d\n", mat[i][j]);
                 }
          }
      } 
  </code></pre>