首页 > 解决方案 > 我在 C 中的数组的函数调用中遇到错误?

问题描述

使用数组在 C 中进行函数调用时出现意外错误,我对 C 有一点了解,但不明白如何修复它。下面附上main函数的代码。

void sum_matrices (int m1[][NUM_COLS], int m2[][NUM_COLS], int num_rows, int result[][NUM_COLS]);
void print_matrix (int m[][NUM_COLS], int num_rows);
void print_array (int a[], int len);

int main(void) {

    int my_matrix_1[][NUM_COLS] = {{1, 5, 3, 4, 2},
                                   {7, 1, 4, 1, 7},
                                   {6, 9, 2, 0, 5}};
    int my_matrix_2[][NUM_COLS] = {{7, 8, 3, 3, 4},
                                   {1, 3, 7, 8, 2},
                                   {1, 3, 5, 6, 0}};
    int num_rows = 3;
    int result[num_rows][NUM_COLS]; // finish this line of code to create the result matrix to pass to sum_matrices

    // add call to sum_matrices to add my_matrix_1 and my_matrix_2

    sum_matrices (my_matrix_1[][NUM_COLS], my_matrix_2[][NUM_COLS], num_rows, result[][NUM_COLS]);

    print_matrix(my_matrix_1, num_rows);
    printf("\n");
    print_matrix(my_matrix_2, num_rows);
    printf("\n");
    print_matrix(result, num_rows);
    return 0;
}

标签: carrays

解决方案


sum_matrices您在函数中对函数的“调用” main...

sum_matrices (my_matrix_1[][NUM_COLS], my_matrix_2[][NUM_COLS], num_rows, result[][NUM_COLS]);

是函数的前向声明和函数调用的混合,但两者都不完整,也不允许在该形式的该位置。

要调用该函数,请编写...

sum_matrices (my_matrix_1, my_matrix_2, num_rows, result);

推荐阅读