i have written the below program for matrix mult using pointers and functions.
the code is:
im starting pointer exercises, and executing the pgm in fedora 12.
if i execute the above pgm, i get segmentation fault.
if i execute using the commented lines in the func, i get the foll error:
ex28.c: In function ‘matmul’:
ex28.c:49: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
help in debugging the errors or mistakes
the code is:
Code:
#include<stdio.h> int M = 3,N = 2; main() { int mat1[M][N],mat2[N][M],i,j,res[M][M]; void matmul(int *a,int *b,int *c); printf("\n enter the matrix elements row wise the size is %dx%d \n",M,N); for (i = 0;i < M;i++) for (j = 0;j < N;j++) scanf("%d",&mat1[i][j]); printf("\n enter the second matrix elements of size %dx%d \n",N,M); for (i = 0;i < N;i++) for (j = 0;j < M;j++) scanf("%d",&mat2[i][j]); matmul(&mat1[0][0],&mat2[0][0],&res[0][0]); printf("\n Matrix 1 = \n"); for (i = 0;i < M;i++) { for (j = 0;j < N;j++) printf("%d \t",mat1[i][j]); printf("\n"); } printf("\n Matrix 2 = \n"); for (i = 0;i < N;i++) { for (j = 0;j < M;j++) printf("%d \t",mat2[i][j]); printf("\n"); } printf("\n Result = \n"); for (i = 0;i < M;i++) { for (j = 0;j < M;j++) printf("%d \t",res[i][j]); printf("\n"); } } void matmul(int *a,int *b,int *c) { int i,j,k,d = sizeof(int); printf("\n size of int = %d \n",d); for (i = 0;i < M;i++) { for (j = 0;j < M;i++) { *(c + i * d + j) = 0; //*(*(c + i) + j) = 0; for (k = 0;k < N;k++) *(c + i * d + j) += (*(a + i * d + k)) * (*(b + k * d + j)); //*(*(c + i) + j) += (*(*(a + i) + k)) * (*(*(b + k) + j)); } } }
if i execute the above pgm, i get segmentation fault.
if i execute using the commented lines in the func, i get the foll error:
ex28.c: In function ‘matmul’:
ex28.c:49: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
help in debugging the errors or mistakes
Comment