将二维数组传递给 c 中的函数时出错

Error when passing 2D array to function in c

我正在编写一个程序来计算矩阵乘法,但它不起作用。当我调试和检查函数 printMatrixMultiplication 中数组 ab 的每个值(由用户输入)时,GDB 打印出 "cannot perform pointer math on不完整类型尝试铸造。 (我已经搜索过了,但我还是不明白。)该功能仅在 main.

中预定义输入时有效

这是我的代码

#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);
}

那么,错误从何而来,如何解决?

无关紧要,但该功能没有很好地实现,所以如果可能的话,如果有人能给我一些改进的提示,我将不胜感激。

我使用的是 GCC 版本 6.3.0。

读取矩阵 b 时你的代码中有错字。

只需替换:

input(row_b, col_b, a);

input(row_b, col_b, b);