title: Copying a pointer of a pointer (matrix) values in C
body: <p>The problem is the following:
  I created a dynamic matrix, using pointer to pointer <code>matrix1</code></p>
  
  <p>I want to create a copy of this matrix into another, <code>matrix2</code></p>
  
  <p>I want to do that so I can mess with <code>matrix2</code> without messing with <code>matrix1</code>
  So I tried to do the following:</p>
  
  <pre><code>int main()
  {
      int **matrix1, **matrix2, size1 = 10, size2 = 2;
      matrix1 = create_matrix(size1, size2);
  
      //I want to copy the value of matrix1 into matrixq2 and NOT the index
      **matrix2 = **matrix1
  }
  </code></pre>
  
  <p>However the program breaks and show the following: <img src="https://i.stack.imgur.com/PRJjD.png" alt="Error"></p>
  
  <p>I understand that by the way it look, it would be easier to use the function <code>create_matrix</code> two times, for <code>matrix1</code> and another for <code>matrix2</code>. But in the way that my original program is that would be too much work, since I do a lot of stuff to have the matrix done. 
  Oh, and by the way, I want to avoid using C++, is there is a way to do it without using it? It would be better for me.</p>
  
  <p>the code 'create_matrix' is the following:</p>
  
  <pre><code>//The program will read a file with the name of endereco, and create a matrix contPx3 out of it
  int ** cria_matrix(char endereco[], int contP)
  {
      FILE *fPointer;
      int i, contE, auxIndex, auxNum, **processos, cont_line = 0;
      char line[100];
      bool flag = true, flag2;
  
      fPointer = fopen(endereco, "r");
  
  //Here the creation of the matrix
      processos = (int**)malloc(sizeof(int*) * contP);
      for (i = 0; i &lt; contP; i++)
          processos[i] = malloc(sizeof(int) * 3);
  
  
  
  //For now and on, is the rules of how the data will be placed on the matrix
      contP = 0;
      while (!feof(fPointer) &amp;&amp; flag)
      {
  
          memset(&amp;line[0], 'Ì', sizeof(line));
          fgets(line, 100 , fPointer);
  //Bassicaly is that in each line there will be 3 numbers only, diveded but as many spaces you want. The numbeer will be placed on the matrix on the determined line they are.
          auxIndex = 0;
          flag2 = false;
          if(line[0] != '#')
              for (i = 0; i &lt; 100; i++)
              {
                  if (line[i] != ' ' &amp;&amp; line[i] != '\n' &amp;&amp; line[i] != '\0' &amp;&amp; line[i] != 'Ì')//&amp;&amp;  line[i] != 'à'
                  {
                      auxNum = line[i] - '0';
                      processos[contP][auxIndex] = auxNum;
                      auxIndex++;
                      flag2 = true;
  
                  }
              }
  
          if (flag2)
              contP++;
  
  
  
          cont_line++;
          if (auxIndex != 3 &amp;&amp; auxIndex != 0)
          {
              flag = false;
              printf("ERRO na linha: %d No processo: %d\nProvavelmente mais ou menos que 3 numeros separado por espacos\n", cont_line, contP);
          }
  
      }
      fclose(fPointer);
      if (!flag)
          system("PAUSE");
      return processos;
  }
  </code></pre>