Home > OS >  Error when passing 2D array to function in c
Error when passing 2D array to function in c

Time:11-26

I am writing a program to calculate matrix multiplication but it does not work. When I debug and check each value of the array a and b in function printMatrixMultiplication (which are entered by user), GDB prints out "cannot perform pointer math on incomplete type try casting". (I have searched for it but I still don't get it.) The function only works when the input is predefined in main.

This is my code

#include <stdio.h>

void input(int m, int n, double a[m][n]);
void output(int m, int n, double a[m][n]);
void printMatrixMultiplication(int row_a, int col_a, double a[row_a][col_a], int row_b, int col_b, double b[row_b][col_b]);

int main()
{
    int row_a, col_a, row_b, col_b;

    // get value of matrix a
    printf("row_a = ");
    scanf("%d", &row_a);
    printf("col_a = ");
    scanf("%d", &col_a);
    double a[row_a][col_a];
    input(row_a, col_a, a);
    // output(row_a, col_a, a);


    // get value of matrix b
    printf("row_b = ");
    scanf("%d", &row_b);
    printf("col_b = ");
    scanf("%d", &col_b);
    double b[row_b][col_b];
    input(row_b, col_b, a);
    // output(row_b, col_b, a);


    printMatrixMultiplication(row_a, col_a, a, row_b, col_b, b);

    //test
    // double a[2][2]={1,2,3,4};
    // double b[2][3]={1,2,3,4,5,6};
    // printMatrixMultiplication(2,2,a,2,3,b);
    return 0;
}

void input(int m, int n, double a[m][n])
{
    int i, j;
    for (i = 0; i < m; i  )
    {
        for (j = 0; j < n; j  )
        {
            scanf("%lf", &a[i][j]);
        }
    }
}

void output(int m, int n, double a[m][n])
{
    int i, j;
    for (i = 0; i < m; i  )
    {
        for (j = 0; j < n; j  )
        {
            printf("%.2f ", a[i][j]);
        }
        printf("\n");
    }
}

void printMatrixMultiplication(int row_a, int col_a, double a[row_a][col_a], int row_b, int col_b, double b[row_b][col_b])
{
    if (col_a != row_b)
    {
        return;
    }
    
    double res[row_a][col_b];           //this matrix store results

    for (int i = 0; i < row_a; i  )                 //the values be stored line by line, this 
    {                                               //operation is controled by i and j loops.
        for (int j = 0; j < col_b; j  )             //the k loop helps calculate dot_product.
        {
            double dot_product = 0;
            for (int k = 0; k < col_a; k  )
            {
                dot_product  = a[i][k] * b[k][j];   //ERROR HERE
            }
            res[i][j] = dot_product;
        }
    }
    output(row_a, col_b, res);
}

So, where does the error come from and how to fix it?

Irrelevant, but the function is not well implemented so if possible, I would really appreciate if anyone gives me a hint to improve it.

I am using GCC version 6.3.0.

CodePudding user response:

It's typo in your code when reading matrix b.

Just replace:

input(row_b, col_b, a);

with

input(row_b, col_b, b);
  • Related