# C program 2D matrix multiplication using malloc

• 04-02-2009
college_kid
C program 2D matrix multiplication using malloc
Hear is a program that I created that already has Matrix A and B filled in. I then created a driver program to create Matrix C and fill in with Matrix A*B. My problem is that I'm getting a Segmentation fault. I know this has to deal with memory that doesnt belong to me, but I have looked around on this site and others and my code looks to be fine in my eyes. LOL must not be very good then. Please help..very confused on where my segmentation fault is happening at. I believe its with a for loop that I have created.

Code:

``` #include <stdlib.h> #include <stdio.h> #include <math.h> int **MatrixMultiply(int **A, int **B, int n); void printMatrix(int **array, int rows, int columns); int main(void) { int i=0; int j=0; //Stores Matrix A #s int **A; A=(int**) malloc(5*sizeof(int*)); for (i=0;i<5;i++) { A[i]=(int*) malloc(5*sizeof(int)); A[0][0]=3,A[0][1]=1,A[0][2]=5,A[0][3]=42,A[0][4]=35; A[1][0]=8,A[1][1]=34,A[1][2]=64,A[1][3]=21,A[1][4]=68; A[2][0]=61,A[2][1]=41,A[2][2]=55,A[2][3]=32,A[2][4]=87; A[3][0]=17,A[3][1]=14,A[3][2]=53,A[3][3]=52,A[3][4]=12; A[4][0]=35,A[4][1]=54,A[4][2]=15,A[4][3]=12,A[4][4]=12; } //Stores Matrix B #s int **B; B =(int**) malloc(5*sizeof(int*)); for(j=0; j<5;j++) { B[i]=(int*) malloc(5*sizeof(int)); B[0][0]=16,B[0][1]=10,B[0][2]=5,B[0][3]=74,B[0][4]=35; B[1][0]=71,B[1][1]=8,B[1][2]=68,B[1][3]=38,B[1][4]=98; B[2][0]=13,B[2][1]=31,B[2][2]=51,B[2][3]=42,B[2][4]=74; B[3][0]=22,B[3][1]=61,B[3][2]=35,B[3][3]=67,B[3][4]=43; B[4][0]=54,B[4][1]=47,B[4][2]=12,B[4][3]=11,B[4][4]=33; } //prints matrix with for loop for(i=0;i<5;i++) {  printf("%d",**MatrixMultiply(A,B,5)); } printf("\n"); //prints matrix with print function //printf("%d",printMatrix(C,5,5)); return (0); } int **MatrixMultiply(int **A, int **B, int n) { int i; int j; int k; int sum; int **C; C=(int**) malloc(5*sizeof(int*)); for (i=0;i<5;i++) { C[i]=(int*) malloc(5*sizeof(int)); } for (i=0; i<n; i++) {  for (j=0; j<n; j++)  {   sum = 0;   for(k=0;k<n;k++)   {   sum += A[i][k] * B[k][j];   }   C[i][j]=sum;  } } return(C); } void printMatrix(int **array, int rows, int columns) {   int i, j;   for(i=0; i<rows; i++)   {     printf("[ ");     for(j=0; j<columns; j++)     {         printf("%5d ", array[i][j]);     }     printf("]\n");   } }```
• 04-02-2009
itCbitC
Before initializing the dynamic array, malloc() storage for all the int items, as in.
Code:

```for (i=0;i<5;i++)     A[i]=(int*) malloc(5*sizeof(int)); A[0][0]=3,A[0][1]=1,A[0][2]=5,A[0][3]=42,A[0][4]=35; A[1][0]=8,A[1][1]=34,A[1][2]=64,A[1][3]=21,A[1][4]=68; A[2][0]=61,A[2][1]=41,A[2][2]=55,A[2][3]=32,A[2][4]=87; A[3][0]=17,A[3][1]=14,A[3][2]=53,A[3][3]=52,A[3][4]=12; A[4][0]=35,A[4][1]=54,A[4][2]=15,A[4][3]=12,A[4][4]=12;```
• 04-03-2009
college_kid
I'm a little confused on what you are trying to explain to me. The way I took what you where trying to say is that my code for that segment should look like this:
Code:

```for(j=0; j<5;j++) { B[i]=(int*) malloc(5*sizeof(int)); } B[0][0]=16,B[0][1]=10,B[0][2]=5,B[0][3]=74,B[0][4]=35; B[1][0]=71,B[1][1]=8,B[1][2]=68,B[1][3]=38,B[1][4]=98; B[2][0]=13,B[2][1]=31,B[2][2]=51,B[2][3]=42,B[2][4]=74; B[3][0]=22,B[3][1]=61,B[3][2]=35,B[3][3]=67,B[3][4]=43; B[4][0]=54,B[4][1]=47,B[4][2]=12,B[4][3]=11,B[4][4]=33;```
When I did this to my program it still gave me a segmentation fault. So again I'm kinda confused as to what you are trying to explain to me.
• 04-03-2009
matsp
This:
Code:

`A[0][0]=3,A[0][1]=1,A[0][2]=5,A[0][3]=42,A[0][4]=35;`
can also be written as:
A[0][0]=(3,A[0][1]=(1,A[0][2]=(5,A[0][3]=(42,A[0][4]=35))));
[/code] (subject to right number of end parenthesis).

Another way to write the exact same functionality it would be:
Code:

`A[0][0]=A[0][1]=A[0][2]=A[0][3]=A[0][4]=35;`
since the comma operator evaluates to the last argument. So you are setting ALL elements to the last assigned value.

My guess is that you didn't REALLY mean that when you wrote the code.

--
Mats
• 04-03-2009
itCbitC
Quote:

Originally Posted by college_kid
I'm a little confused on what you are trying to explain to me. The way I took what you where trying to say is that my code for that segment should look like this:
Code:

```for(j=0; j<5;j++) { B[i]=(int*) malloc(5*sizeof(int)); } B[0][0]=16,B[0][1]=10,B[0][2]=5,B[0][3]=74,B[0][4]=35; B[1][0]=71,B[1][1]=8,B[1][2]=68,B[1][3]=38,B[1][4]=98; B[2][0]=13,B[2][1]=31,B[2][2]=51,B[2][3]=42,B[2][4]=74; B[3][0]=22,B[3][1]=61,B[3][2]=35,B[3][3]=67,B[3][4]=43; B[4][0]=54,B[4][1]=47,B[4][2]=12,B[4][3]=11,B[4][4]=33;```
When I did this to my program it still gave me a segmentation fault. So again I'm kinda confused as to what you are trying to explain to me.

Can you spot the difference??
• 04-03-2009
college_kid
Thank you, That makes much more sense. Didn't realize you couldn't declare like that. It worked. Now just have to take care of a couple bugs...yippy.

Thanks