title: Using realloc on a 2D array c
body: <p>I'm kinda new to C sorry if my questions is somewhat vague;</p>
  
  <p>I need to use realloc on a 2D array without losing it's previous data, I have this function in my program to do it:</p>
  
  <pre><code>void modifyMatrix(int **iMat, int iRow, int iRow2, int iCol)
  {
      int i;
      iMat = (int**)realloc(iMat, (iRow2)*sizeof(int*));
      for(i=iRow; i&lt;iRow2; i++)
      {
          iMat[i]=NULL;
      }
      for(i=0; i&lt;iRow2; i++)
      {
          iMat[i]=(int*)realloc(iMat[i], (iCol)*sizeof(int));
      }
  }
  </code></pre>
  
  <p>Where iRow is the original size and iRow 2 &amp; iCol are the new size and are all being captured elsewhere in the program.</p>
  
  <p>Whenever I try to print the matrix I keep getting junk data or memory values on the rows and columns that are added, what am I doing wrong?</p>
  
  <p>Let me know if you need the full code or any other questions to clarify, thanks in advance!</p>
  
  <p>Edit:
  Below you can see the code I use to create the Matrix</p>
  
  <p>My bad, I think I should've added that the Matrix is already created elsewhere in the program, with this function I'm just trying to modify the dimensions, thanks for the quick response btw!, below you can find the function with which I'm creating the array</p>
  
  <pre><code>void createMatrix(int ***iMat, int iRow, int iCol)
  {
      int **iRow2 = (int**)calloc(iRow, sizeof(int*));
      int i;
      for (i=0; i&lt;iRow; i++)
      {
          iRow2[i] = (int*)calloc(iCol, sizeof(int));
      }
      *iMat=iRow2;
  }
  </code></pre>
  
  <p>Also, I can only use the array I've already created to do this, I can't create an temp one (which I know would be the easy way to do it)</p>