Home > Software engineering >  facing issue when multiplying 2d matrices in c
facing issue when multiplying 2d matrices in c

Time:04-26

So I am new to the C language trying to multiply 2d arrays via pointer by passing it to a function

Programm exits when it is inside multiplication function, only prints "I am in function"

Plz 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 coloumns matrix 1
int x, y;           // rows and coloumns 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 coloumn for matrix 1\n");
    scanf("%d %d", &m, &n);
    printf("Enter the rows and coloumn 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 coloumn for matrix 1
2
2
Enter the rows and coloumn 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

CodePudding user response:

You treat an array as if it was the same as a pointer. This is not true.

Your array holds n*m integers that are stored in memory at consecutive addresses. You tell the compiler that parameter arr holds the address of a pointer to an integer. This means, if you write arr[i][j] will dereference your pointer and read an address from memory that is also dereferenced. This address is actually an int in memory, not a pointer. Taking an integer and use as address causes undefined behaviour.

Luckily your compiler already told you that these types do not match. It also provides the correct type of your argument. With this information you can fix the signature:

void multiplication(int n, int y, int (*arr)[n], int (*arr1)[y])

Then update your function call accordingly.

Also you have a typo in one of your loops:

for (int j = 0; i < n; j  )

That should be j < n.

CodePudding user response:

  1. Avoid global variables. That is, the declarations of x, y, m, n should be placed inside main() and then passed as parameters to the function.
  2. Pointer-to-pointer has absolutely nothing to do with 2D arrays.

After fixing these problems, your function might look like this instead:

void multiplication (int x, int y, int arr1[x][y], int arr2[x][y])

In case the function isn't going to modify either array, it is custom to declare it with const correctness:

void multiplication (int x, int y, const int arr1[x][y], const int arr2[x][y])
  •  Tags:  
  • c
  • Related