title: Using Malloc to add two arrays HW in C
body: <p>This program is supposed to  ask the user to enter two matrices and provide the sum of the two. When compiled it does not work as expected, I believe it is due to my use of <code>malloc</code>, if anyone can help this would be greatly appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;malloc.h&gt;
  
  
  /*Here we declare and define our function 'matrices', which prompts the
   user for Matrix A and Matrix B and stores their values.*/
  
  int matrices(int rows, int cols){
  
      int i;
  
      int** matrixA = malloc(rows * sizeof(**matrixA));
  
      int** matrixB = malloc(rows * sizeof(**matrixB));
  
      printf("Enter Matrix A\n");
  
      for (i = 0; i &lt; rows; i++){
          matrixA[i] = (int *) malloc(cols * sizeof(int));
  
           scanf("%d", matrixA[i]);
  
      }
  
      printf("Enter Matrix B\n");
  
      for (i = 0; i &lt; rows; i++){
          matrixB[i] = (int *) malloc(cols * sizeof(int));
  
           scanf("%d", matrixB[i]);
  
      }
  
      return 0;
  }
  
  /*Here we declare and define our function 'sum', which sums Matrix A and 
  Matrix B and provides us with the summation of the two in the 
  variable 'matrixSum'*/
  
  int sum(int rows, int cols){
  
      int i;
  
      int** matrixA = malloc(rows * sizeof(**matrixA));
  
      int** matrixB = malloc(rows * sizeof(**matrixB));
  
      int** matrixSum = malloc(rows * sizeof(**matrixSum));
  
  
  
      printf("A + B =\n");
  
      for (i = 0; i &lt; rows; i++) {
  
          matrixA[i] = (int *) malloc(cols * sizeof(int));
          matrixB[i] = (int *) malloc(cols * sizeof(int));
          matrixSum[i] = (int *) malloc(cols * sizeof(int));
  
             *matrixSum[i] = *matrixA[i] + *matrixB[i];
          printf("%d\t", *matrixSum[i]);
  
  
              printf("\n");
      }
  
      return 0;
  }
  /*Here we declare and define our main function which works to prompt the user for the number of rows and columns and calls the previously declared functions. */
  
  int main(void){
  
      int rows, cols;
  
      int** matrixA = malloc(rows * sizeof(**matrixA));
  
      int** matrixB = malloc(rows * sizeof(**matrixB));
  
      int** matrixSum = malloc(rows * sizeof(**matrixSum));
  
      printf("Please enter the number of rows: ");
      scanf("%d", &amp;rows);
      printf("Please enter the number of columns: ");
      scanf("%d", &amp;cols);
  
  
      matrices(rows, cols);
      sum(rows, cols);
  
      free(matrixA);
      free(matrixB);
      free(matrixSum);
  
      return 0;
  }
  </code></pre>