error while executing a matrix multiplication program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • girishc13
    New Member
    • Apr 2010
    • 3

    error while executing a matrix multiplication program

    i have written the below program for matrix mult using pointers and functions.
    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));
    }
    }
    }
    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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The calling aruments of your matmul function are int*. This is not a 2D array but is only the address of a single int.

    There is a correct way to pass the address of a 2D array to a function and that is to pass the address of element 0. In a 2D array element 0 is itself an array so you need to pass the address of the array that is the second dimension.

    This array:

    Code:
    int arr[3][4];
    is passed to a function using the argument of a pointer to an array of 4 int:

    Code:
    void MyFunc(int (*arg)[4]), int num);
    You also need to pass in the number of elements. In this case the number of 4 int arrays.


    Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

    Comment

    • girishc13
      New Member
      • Apr 2010
      • 3

      #3
      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)[N],int (*b)[M],int (*c)[M]);
      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,mat2,res);
      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)[N],int (*b)[M],int (*c)[M])
      {
      int i,j,k,d = sizeof(int);
      printf("\n size of int = %d \n",d);
      or (i = 0;i < N;i++)
      for (j = 0;j < M;j++)
      {
      printf("%d \t",(*(a + i))[j]);
      }
      for (i = 0;i < M;i++)
      {
      for (j = 0;j < M;i++)
      {
      (*(c + i))[j] = 0;
      for (k = 0;k < N;k++)
      (*(c + i))[j] += (*(a + i))[k] * (*(b + k))[j];
      }
      }
      }
      ok i changed the pgm for passing the array parameters.but i still get segmentation fault. and cant print the array in the func using pointers

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        and cant print the array in the func using pointers
        ?
        If you can't see the output it's because crash occurs before the output stream is flushed. As for seg fault itself, the reason may be an array index out of bound - then icorrect pointer is fetched and when it's accessed, crash occurs. Did you localize it?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Your code compiled and executed OK on Visual Studio.NET 2008. I didn;t check that the answer was correct but therewas no crash.

          I did have to make M and N const and I did have to make main() return an int.

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            It crashes because 'i' runs out of range.

            Comment

            • girishc13
              New Member
              • Apr 2010
              • 3

              #7
              its working now thanx for ur replies,heres the working pgm:

              #include<stdio. h>
              int M = 3,N = 2;
              void main()
              {
              int mat1[M][N],mat2[N][M],i,j,res[M][M];
              void matmul(int ar1[][N],int ar2[][M],int ar3[][M]);
              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",&mat 1[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",&mat 2[i][j]);
              matmul(mat1,mat 2,res);
              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 ar1[][N],int ar2[][M],int ar3[][M])
              {
              int i = 0,j = 0,k = 0,d = sizeof(int),(*a )[N] = ar1,(*b)[M] = ar2,(*c)[M] = ar3;
              printf("\n size of int = %d \n",d);
              printf("\n size of int = %d \n",d);
              for (i = 0;i < M;i++)
              {
              for (j = 0;j < M;j++)
              {
              (*(c + i))[j] = 0;
              printf("%d \t",(*(c + i))[j]);
              }
              }
              for (i = 0;i < M;i++)
              {
              for (j = 0;j < M;j++)
              {
              (*(c + i))[j] = 0;
              for (k = 0;k < N;k++)
              {
              (*(c + i))[j] += (*(a + i))[k] * (*(b + k))[j];
              }
              }
              }
              return;
              }

              'i' was going out of range cos i was incremented in the j loop also. newbie mistake.the lesson was very good for arrays to function parameter passing.

              Comment

              Working...