1

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.

6
  • Make sure that you compile as standard C and not some obsolete version of the language. Which compiler and version are you using and what compiler options? Commented Nov 26, 2021 at 13:55
  • Compiles without error for me. Edit the question to provide a minimal reproducible example, including the specific compiler, version, and switches used. Commented Nov 26, 2021 at 13:56
  • @Lundin I am using gcc 6.3.0 Commented Nov 26, 2021 at 13:56
  • it compiles fine with GCC-9.3, even in pedantic mode, what is the exact error? Commented Nov 26, 2021 at 14:00
  • @tstanisl I debug to check the value of array and it print out this "cannot perform pointer math on incomplete type try casting" Commented Nov 26, 2021 at 14:02

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

So apparently "cannot perform pointer math on incomplete type" is a message you get from GDB whenever you allocate a VLA with one size then tell it to access it with a different size - potentially out of bounds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.