title: Dynamically allocate matrix C for multiplication
body: <p>I am trying to code for matrix multiplication of square matrices and it will keep giving Segmentation fault after every few entires on several trials. 
  I looked up different question on the site and tried few ways around with two following codes.</p>
  
  <p>Also, Why do we need the pointers to "pointer to a pointer" as given by int **mat1, **mat2 etc. ? I don't know why this is to be done but I saw it in some answer itself.</p>
  
  <p>Code 1</p>
  
  <pre><code>void mxmult()
  {
      int n,m,a,b,c,d, sum=0;
      int x,y,z;
      printf("Enter first order [n*n]\n");
      scanf("%d", &amp;n);
      printf("Enter second order [m*m]\n");
      scanf("%d", &amp;m);
      if (n!=m)
      {
          printf("Invalid orders");
  
      }
      else
      {
          //mem allocate for matrix 1
          int **mat1 = (int**)malloc(n*sizeof(int));
          for(x=0;x&lt;n;x++)
              {
                  mat1[x]=(int*)malloc(n*sizeof(int));
              }
          // input matrix 1
          printf("Enter the first matrix entries\n");
          for (a = 0; a &lt;n; a++)
          {
              for (b = 0; b &lt; n; b++)
              {
                  scanf("%d", &amp;mat1[a][b]);   
              }
          }
          // memory allocate matrix 2
          int **mat2 = (int**)malloc(m*sizeof(int));
          for(y=0;y&lt;n;y++)
              {
                  mat2[y]=(int*)malloc(m*sizeof(int));
              }
  
          //inpur matrix 2
          printf("Enter the second matrix entries\n");
          for (c = 0; c &lt;n; c++)
          {
              for (d= 0; d &lt; n; d++)
              {
                  scanf("%d", &amp;mat2[c][d]);   
              }
          }
  
          //Memory allocate matrix Mult
          int **mult=(int**)malloc(m*sizeof(int));
          for(z=0;z&lt;m;z++)
              mult[z]=(int*)malloc(m*sizeof(int));
          for (a = 0; a &lt; n; a++)
          {
              for (d = 0; d &lt; m; d++)
              {
                  for (c = 0; c &lt; n; c++)
                  {
                      sum=sum + (mat1[a][c] *mat2[c][d]);
                  }
                  mult[a][d] = sum;
                  sum= 0;
              }
          }
          printf("Product\n");
  
          for ( a = 0 ; a &lt; n ; a++ )
          {
              for ( d = 0 ; d &lt; m ; d++)
                  printf("%d\t", mult[a][d]);
              printf("\n");
          }
  
      }
  }  
  </code></pre>
  
  <p>Code 2:</p>
  
  <pre><code>void mxmult()
  {
      int n,m,a,b,c,d, sum=0;
      int x,y,z;
      printf("Enter first order [n*n]\n");
      scanf("%d", &amp;n);
      printf("Enter second order [m*m]\n");
      scanf("%d", &amp;m);
      if (n!=m)
      {
          printf("Invalid orders");
  
      }
      else
      {
          //mem allocate for matrix 1
          int **mat1 = (int**)malloc(n*n*sizeof(int));
  
          // input matrix 1
          printf("Enter the first matrix entries\n");
          for (a = 0; a &lt;n; a++)
          {
              for (b = 0; b &lt; n; b++)
              {
                  scanf("%d", &amp;mat1[a][b]);   
              }
          }
          // memory allocate matrix 2
          int **mat2 = (int**)malloc(m*m*sizeof(int));
  
            //input matrix 2
          printf("Enter the second matrix entries\n");
          for (c = 0; c &lt;n; c++)
          {
              for (d= 0; d &lt; n; d++)
              {
                  scanf("%d", &amp;mat2[c][d]);   
              }
          }
  
          //Memory allocate matrix Mult
          int **mult=(int**)malloc(m*m*sizeof(int));
  
              // Mx multiplicatn
          for (a = 0; a &lt; n; a++)
          {
              for (d = 0; d &lt; m; d++)
              {
                  for (c = 0; c &lt; n; c++)
                  {
                      sum=sum + (mat1[a][c] *mat2[c][d]);
                  }
                  mult[a][d] = sum;
                  sum= 0;
              }
          }
          printf("Product\n");
  
          for ( a = 0 ; a &lt; n ; a++ )
          {
              for ( d = 0 ; d &lt; m ; d++)
                  printf("%d\t", mult[a][d]);
              printf("\n");
          }
  
      }
  }  
  </code></pre>
  
  <p>I have been trying to execute code 2 and then, code2 . Both end up giving seg faults after few entires.</p>