title: How to multiply matrix using pointers - C
body: <p>I have to write a program which multiply matrix using pointers.
  My program does it correctly, but there are two problems:</p>
  
  <p>1) I can do only one multiplication - when I want next one there is error</p>
  
  <p>2) when I input data of first and second matrix i have to put one more
   char to make my program work.</p>
  
  <p>What should I change in my code to solve those problems?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  while (z--) {               // the number of sets
      scanf ("%d %d", &amp;n1, &amp;m1);      // first matrix n1 x m1
  
      int **A = malloc(n1 * sizeof(int*));
      for (i = 0; i &lt; n1; i++) {
          *(A+i) = malloc(m1 * sizeof(int));
      }
  
      for (i = 0; i &lt; n1; i++) {
          for (j = 0; j &lt; m1; j++) {
              scanf("%d ", (A+i*m1+j));
          }   
      }
  
      scanf("%d %d", &amp;n2, &amp;m2);       // second matrix n2 x m2
  
      int **B = malloc(n2 * sizeof(int*));
      for (i = 0; i &lt; n2; i++) {
          *(B+i) = malloc(m2 * sizeof(int));
      }
  
      for (i = 0; i &lt; n2; i++) {
          for (j = 0; j &lt; m2; j++) {
              scanf("%d ", (B+i*m2+j));
          }
      }
  
      if (m1 != n2) {
          printf ("ERROR\n");
      } else {
          int **C = malloc(n1 * sizeof(int*));        //matrix with product
          for (i = 0; i &lt; n1; i++) {
              *(C+i) = malloc(m2 * sizeof(int));
          }
  
          for (i=0; i&lt;n1; i++) {
              for (j=0; j&lt;m2; j++) {
                  *(C+i*m2+j) = 0;
              }
          }
  
          for (k = 0; k &lt; n2; k++) {
              for (i = 0; i &lt; n1; i++) {
                  for (j = 0; j &lt; m2; j++) {
                      C1 = 0;
                      A1 = *(A+i*m1+k);
                      B1 = *(B+k*m2+j);
                      C1 = (C1+A1*B1)%1000;
                      temp = *(C+i*m2+j);
                      *(C+i*m2+j) = temp+C1;
                  }
              }
          }
  
          for (i = 0; i &lt; n1; i++) {
              for (j = 0; j &lt; m2; j++) {
                  printf("%d ", *(C+i*m2 +j));
              }
              printf("\n");
          }
  
          for (i = 0; i &lt; n1; i++) {      // free third matrix
              free(*(C+i));
          }
          free(C);
      }
  
      for (i = 0; i &lt; n1; i++) {          // free first matrix
          free(*(A+i));
      }
      free(A);
  
      for (i = 0; i &lt; n2; i++) {              // free second matrix
          free(*(B+i));
      }
      free(B);
  }
  return 0;
  </code></pre>