title: Matrix (2d array) and its values is not written into txt file correctly
body: <p>In my program, I enable the user to decide the dimensions of their matrix and then input each cell's value in the matrix. This matrix is then saved to a .txt file as binary data. Sometimes, with certain matrices of certain values and dimensions, the matrix is not read back properly with the correct values in the correct cells.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;dirent.h&gt;
  
  int numberOfMatrices = 0;
  int orderedMatricesInfo[100][1][2];
  int matrixRows = 0;
  int matrixCols = 0;
  
  int** initMatrix(int rows, int cols);
  void saveMatrix(int** matrix, int rows, int cols);
  void* allocateMatrix(int rows, int cols);
  int** readMatrix(int rows, int cols, int matrix[matrixRows][matrixCols], int matrixNum);
  void displayMatrices();
  void displayMatrix(int rows, int cols, int** matrix);
  
  int main()
  {
  
      int n, m;
      int** matrixPointer;
  
      printf(&quot;Please enter the number of rows in your matrix: \n&quot;);
      scanf(&quot;%d&quot;, &amp;n);
  
      printf(&quot;Please enter the number of columns in your matrix: \n&quot;);
      scanf(&quot;%d&quot;, &amp;m);
  
      matrixPointer = initMatrix(n, m);
  
      printf(&quot;%d\n&quot;, matrixPointer[0][0]);
  
      displayMatrices();
  
      printf(&quot;SUCCESS&quot;);
  }
  
  int** initMatrix(int rows, int cols)
  {
      int matrix[rows][cols];
      int **matrixPointer;
  
      matrixPointer = malloc(sizeof(int*)*cols);
  
      for (int i =0; i &lt; cols; ++i)
      {
          matrixPointer[i] = malloc(sizeof(int*)*cols);
      }
  
      for (int i = 0; i &lt; rows; ++i)
      {
          for (int j = 0; j &lt; cols; ++j)
          {
              printf(&quot;\nPlease enter element for [%d][%d]: \n&quot;, i, j);
              scanf(&quot;%d&quot;, &amp;matrixPointer[i][j]);
          }
      } 
  
      ++numberOfMatrices;
  
      orderedMatricesInfo[numberOfMatrices-1][0][0] = rows;
      orderedMatricesInfo[numberOfMatrices-1][0][1] = cols;
  
      allocateMatrix(rows, cols);
  
      saveMatrix(matrixPointer, rows, cols);
      return matrixPointer;
  }
  
  void saveMatrix(int** matrix, int rows, int cols)
  {   
      char fullFileName[100] = &quot;matrix&quot;;
      char fileNameExtension[100] = &quot;.txt&quot;;
      char strNum[100];
  
      sprintf(strNum, &quot;%d&quot;, numberOfMatrices);
  
      strcat(strNum, fileNameExtension);
      strcat(fullFileName, strNum);
  
      FILE *file = fopen(fullFileName, &quot;ab&quot;);
  
      for(int i=0; i&lt;rows; ++i)
      {
          fwrite(matrix[i], sizeof(int), rows, file);
      }
      fclose(file);
  }
  
  void* allocateMatrix(int rows, int cols)
  {
      return malloc(sizeof(int[rows][cols]));
  }
  
  int** readMatrix(int rows, int cols, int matrix[matrixRows][matrixCols], int matrixNum) 
  {
      int **matrixPointer;
      matrixPointer = malloc(sizeof(int*)*cols);
  
      for (int i =0; i &lt; cols; ++i)
      {
          matrixPointer[i] = malloc(sizeof(int*)*cols);
      }
  
      char fullFileName[100] = &quot;matrix&quot;;
      char fileNameExtension[100] = &quot;.txt&quot;;
      char strNum[100];
  
      sprintf(strNum, &quot;%d&quot;, matrixNum);
  
      strcat(strNum, fileNameExtension);
      strcat(fullFileName, strNum);
  
      FILE *file;
      file=fopen(fullFileName, &quot;rb&quot;);
      fread(matrix, sizeof(int[rows][cols]), 1, file); // read 1 2D-array
  
      for (int i = 0; i &lt; rows; ++i)
      {
          for (int j = 0; j &lt; cols; ++j)
          {
              matrixPointer[i][j] = matrix[i][j];
              printf(&quot;%d&quot;, matrixPointer[i][j]);
          }
      }
  
      allocateMatrix(rows, cols);
  
      return matrixPointer;
  }
  
  void displayMatrices()
  {
      for (int matrixNum = 1; matrixNum &lt;= numberOfMatrices; ++matrixNum)
      {
          char fullFileName[100] = &quot;matrix&quot;;
          char fileNameExtension[100] = &quot;.txt&quot;;
      
          char strNum[100];
          sprintf(strNum, &quot;%d&quot;, matrixNum);
          strcat(strNum, fileNameExtension);
          strcat(fullFileName, strNum);
  
          matrixRows = orderedMatricesInfo[matrixNum -1][0][0];
          matrixCols = orderedMatricesInfo[matrixNum -1][0][1];
  
          int matrix[matrixRows][matrixCols];
          int** matrixPointer;
  
          matrixPointer = readMatrix(matrixRows, matrixCols, matrix, matrixNum);
  
          printf(&quot;matrix %d&quot;, matrixNum);
          displayMatrix(matrixRows, matrixCols, matrixPointer);
      }
  }
  
  void displayMatrix(int rows, int cols, int** matrix)
  {
      for (int i = 0; i &lt; rows; ++i)
      {
          printf(&quot;\n&quot;);
          for (int j = 0; j &lt; cols; ++j)
          {
              printf(&quot;%d &quot;, matrix[i][j]);
          }
      }
      printf(&quot;\n&quot;);
  }
  </code></pre>
  <p>In the main function, when the user creates the matrix, the matrix is saved in a newly created file named &quot;matrix1.txt&quot; as binary data. However, it is not stored properly I think as when read back, the matrix is not shown correctly with the correct values.
  For example, if I create a matrix:</p>
  <pre><code>[12, 19, 17, 15,
  120, 566, 214, 153,
  124, 1, 2, 3]
  </code></pre>
  <p>And then attempt to display the matrix then the matrix above is not shown, instead this is shown:</p>
  <pre><code>[12, 19, 17, 120,
  566, 214, 124, 1,
  2, 0, 101792956, 1]
  </code></pre>
  <p>Have I assigned memory incorrectly into this program? What would the problem be? As it does manage to get the first 3 elements of the top row retrieved and shown correctly. I am new to C so I cannot pinpoint the problem. When the matrix is smaller like a 2x2 matrix, then it works fine...</p>
  <p>Thank you!</p>