【问题标题】:Passing a two dimensional array to a function in C将二维数组传递给C中的函数
【发布时间】:2014-11-05 19:18:41
【问题描述】:
#include <iostream>
#include <math.h>
#include <stdio.h>
#define column 3
#define row 3
#define share 3

int matrix_multiplication(int left_matrix[][column], int right_matrix[][column], int result_matrix[][column], int rows, int cols, int shared);

int A[][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
},
B[][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
}, C[3][3]; //initialize "hard coded" three matrices

int main() {
    matrix_multiplication(A, B, C, row, column, share); //passes address of each matrix to function
    return 0;
}

int matrix_multiplication(int left_matrix[][column], int right_matrix[][column], int result_matrix[][column], int rows, int cols, int shared) {
    int i, j, k;

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {//stays within first column and row through first iteration
            for (k = 0; k < 3; k++)//ensures inner dimensions match with variable k i.e. ixk * kxj =ixj or A*B=C
                result_matrix[i][j] += right_matrix[i][k] * left_matrix[k][j]; //C programming has ability to perform matrix multiplication
            //Adds result of each operation to C matrix i.e. +=
            printf("%d\t", result_matrix[i][j]); //matrix C is composed of rows(i) and columns(j)
        }//new tab after each column iteration
        printf("\n"); //new line for each row iteration
    }
    return 0;
}

此代码是使用指针将多维数组传递给函数并在乘法运算后打印多维数组的一个很好的示例。有多种方法可以指示指向编译器的指针。我建议查看“将二维数组传递给函数的正确方法”。例如:

/*void display(int (*p)[numcols],int numRows,int numCols)//First method//
void dispaly(int *p,int numRows,int numCols) //Second Method//
void dispaly (int p[][numCols],int numRows,int numCols)  //Third Method*/

【问题讨论】:

  • 二维数组是int**,而不是int*。你在乘法线上遇到了什么错误?
  • @JasonBaker 不,int ** 不是 2d 数组,它是指针数组。
  • 您不能将二维数组作为 c 或 c++ 中的参数作为简单的int arr[][] 参数传递。
  • 请发布完整的原始错误消息,包括文件名和行号。

标签: c arrays function matrix


【解决方案1】:

删除column 变量,并将其添加到matrix_multiplication 函数声明的上方:

#define column 3

(您可能还想将column 重命名为COLUMNS。)

在 C++ 中,您也可以这样做:

static const int column = 3;

或者,在 C++11 中:

constexpr int column = 3;

所有这一切背后的想法是,除了第一个多维数组的大小之外,其他所有内容都必须在编译时知道。


要修复expected primary-expression before ']' token" 错误,请将您的内部分配更改为如下内容:

result_matrix[i][j] += right_matrix[i][k] * left_matrix[k][j];

您还应该先用 0 初始化 result_matrix


同时从int *result_matrix[][column] 中删除*

如果您传递 int 而不是 int*,大多数现代编译器都会显示警告。请启用编译器中的所有警告,重新编译,修复它们,并更新您的问题,说明示例代码编译干净,没有警告。


要打印矩阵的元素,您必须指定要打印的元素:

printf("%d\t", result_matrix[i][j]);

我不敢相信当您省略 [i][j] 时,您的编译器没有显示警告。警告是为了您的利益:它们表明您的代码中可能存在错误。

【讨论】:

  • 可能我没关注,我已经把column变量去掉了,换成了函数原型语句上面的#define column 3指令。
  • int* result_matrix[][column] 也是错的,printf("%d\t", result_matrix); 是勇敢的,但注定要失败。
  • 它是 int* result_matrix[][column]。不敢相信我错过了。 3X3 使用新选项卡 printf 行正确打印,但 3X3 数组中的值都是 4223008。
  • 我在答案中添加了更多建议,并修复了 `+=' 公式中的索引([i][j] 而不是 [i][k])。
  • 感谢您的耐心等待。这是我的第一堂编程课,这个论坛真的很有帮助!这是缺少所需元素的打印语句。
猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多