I am new to the C language trying to multiply 2d arrays via pointer by passing it to a function.
Program exits when it is inside multiplication function, only prints "I am in function".
Please let me know if I am passing 2d array via pointers is correct?
I have mentioned some warnings and output
Thanks
#include <stdio.h>
int m, n; // rows and columns matrix 1
int x, y; // rows and columns matrix 2
void multiplication(int **arr, int **arr1)
{
printf("I am in function");
int result[m][y];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
result[i][j] = (arr[i][j]) * (arr1[i][j]);
}
printf("calculating");
}
for (int i = 0; i < m; i++)
{
for (int j = 0; i < n; j++)
{
printf("printing");
printf("%d", result[i][j]);
printf("\t");
}
printf("\n");
}
}
int main()
{
printf("Enter the rows and column for matrix 1\n");
scanf("%d %d", &m, &n);
printf("Enter the rows and column for matrix 2\n");
scanf("%d %d", &x, &y);
if (n != x)
{
printf("Cant multiply matrix");
}
else if (n == x)
{
int a1[m][n], a2[x][y];
printf("Enter the value for matrix 1 \n\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &a1[i][j]);
}
}
printf("Enter the value for matrix 2 \n\n");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
scanf("%d", &a2[i][j]);
}
}
multiplication(a1, a2);
return 0;
}
}
SOME WARNINGS:
matrixmult.c:59:24: warning: passing argument 1 of 'multiplication' from incompatible pointer type [-Wincompatible-pointer-types]
59 | multiplication(a1, a2);
| ^~
| |
| int (*)[n]
matrixmult.c:5:27: note: expected 'int **' but argument is of type 'int (*)[n]'
5 | void multiplication(int **arr, int **arr1)
| ~~~~~~^~~
matrixmult.c:59:28: warning: passing argument 2 of 'multiplication' from incompatible pointer type [-Wincompatible-pointer-types]
59 | multiplication(a1, a2);
| ^~
| |
| int (*)[y]
matrixmult.c:5:38: note: expected 'int **' but argument is of type 'int (*)[y]'
5 | void multiplication(int **arr, int **arr1)
| ~~~~
OUTPUT:
Enter the rows and column for matrix 1
2
2
Enter the rows and column for matrix 2
2
2
Enter the value for matrix 1
1
2
1
2
Enter the value for matrix 2
1
2
1
2
I am in function