c - 使用函数进行矩阵乘法的警告

标签 c pointers matrix-multiplication

我遇到三个错误:

  1. 赋值从指针生成整数,无需强制转换
  2. 从不兼容的指针类型传递 read 的参数 1
  3. 应为 int * (*)[10] 但参数类型为 int (*)[10][10]

代码如下:

#include <stdio.h>
#include <stdlib.h>

void read(int *(arr[10][10]), int row, int col) { //Third error here
    int i, j;
    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
            scanf("%d", &arr[i][j]);
}
void multiply(int arr1[10][10], int row1, int col1,
              int arr2[10][10], int row2, int col2,
              int *prod[10][10]) { //Third error here
    int i, j, k, temp;
    for (i = 0; i < row1; i++)
        for (j = 0; j < col2; j++) {
            temp = 0;
            for (k = 0; k < col1; k++)
                temp += arr1[i][k] * arr2[k][j];
            prod[i][j] = temp; //First error here
        }
}
void display(int arr[10][10], int row, int col) {
    int i, j;
    for (i = 0; i < row; i++) {
        for (j = 0; j <col; j++)
            printf("%d\t",arr[i][j]);
        printf("\n");
    }
}
int main() {
    int a[10][10], b[10][10], c[10][10], m, n, p, q, i, j, k;
    printf("Enter the order of matrix A:");
    scanf("%d %d", &m, &n);
    printf("Enter the order of matrix B:");
    scanf("%d %d", &p, &q);
    if (n != p) {
        printf("Matrix multiplication is not possible.");
        exit(0);
    }
    printf("Enter the elements of matrix A:\n");
    read(&a, m, n); //Second error here
    printf("Enter the elements of matrix B:\n");
    read(&b, p, q); //Second error here
    multiply(a, m, n, b, p, q, &c);
    printf("Matrix A is:\n");
    display(a, m, n);
    printf("Matrix B is:\n");
    display(b, p, q);
    printf("Product matrix is:\n");
    display(c, m, q);
    return 0;
}

最佳答案

函数参数的类型 int *(arr[10][10])int arr[10][10] 不同。以这种方式更改原型(prototype):

void read_matrix(int arr[10][10], int row, int col)

您不应将函数命名为read,因为它是C 库用来实现FILE* 流接口(interface)的Posix 系统调用的名称。将其重命名为 read_matrix 并以这种方式调用它:

printf("Enter the elements of matrix A:\n");
read_matrix(a,m,n); //Second error here
printf("Enter the elements of matrix B:\n");
read_matrix(b,p,q); //Second error here

矩阵乘法的原型(prototype)应该类似于:

void multiply(int arr1[10][10], int row1, int col1,
              int arr2[10][10], int row2, int col2,
              int prod[10][10])

混淆源于数组在 C 中传递给函数的方式:它们作为指向其第一个元素的指针传递。我们说当在表达式中使用数组并作为函数参数或返回值传递时,数组会自动衰减为指针。 multiply 应该这样调用:

multiply(a, m, n, b, p, q, c);

请注意,对于主体超过一行的所有 for 循环,您应该使用大括号 {}。您在 readmultiply 的外部循环中省略它们并非完全不正确,但容易出错且不推荐。

您还应该检查 scanf() 的返回值并验证矩阵维度。

这是一个改进的版本:

#include <stdio.h>
#include <stdlib.h>

int matrix_read(int arr[10][10], int row, int col) {
    int i, j;

    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            if (scanf("%d", &arr[i][j]) != 1)
                return -1;
        }
    }
    return 0;
}

int matrix_multiply(int arr1[10][10], int row1, int col1,
                    int arr2[10][10], int row2, int col2,
                    int prod[10][10]) {
    int i, j, k, temp;

    if (col1 != row2)
        return -1;

    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            temp = 0;
            for (k = 0; k < col1; k++) {
                temp += arr1[i][k] * arr2[k][j];
            }
            prod[i][j] = temp;
        }
    }
    return 0;
}

void matrix_display(int arr[10][10], int row, int col) {
    int i, j;

    for (i = 0; i < row; i++) {
        for (j = 0; j <col; j++) {
            printf("%d\t", arr[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    int a[10][10], b[10][10], c[10][10], m = 0, n = 0, p = 0, q = 0;

    printf("Enter the order of matrix A: ");
    if (scanf("%d %d", &m, &n) != 2 || m < 1 || m > 10 || n < 1 || n > 10) {
        printf("Invalid matrix size: %d x %d.\n", m, n);
        exit(1);
    }
    printf("Enter the order of matrix B: ");
    if (scanf("%d %d", &p, &q) != 2 || p < 1 || p > 10 || q < 1 || q > 10) {
        printf("Invalid matrix size: %d x %d.\n", p, q);
        exit(1);
    }
    if (n != p) {
        printf("Matrix multiplication is not possible.");
        exit(1);
    }
    printf("Enter the elements of matrix A:\n");
    if (matrix_read(a, m, n)) {
        printf("Invalid matrix data.\n");
        exit(1);
    }
    printf("Enter the elements of matrix B:\n");
    if (matrix_read(b, p, q)) {
        printf("Invalid matrix data.\n");
        exit(1);
    }
    if (matrix_multiply(a, m, n, b, p, q, c)) {
        printf("Matrix multiplication error.\n");
        exit(1);
    }
    printf("Matrix A is:\n");
    matrix_display(a, m, n);
    printf("Matrix B is:\n");
    matrix_display(b, p, q);
    printf("Product matrix is:\n");
    matrix_display(c, m, q);
    return 0;
}

关于c - 使用函数进行矩阵乘法的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35182218/

相关文章:

c - 如何让 getaddrinfo 只返回一个结果?

c - 打印 0x85 或 0x95 时的未定义行为

c - 将参数指针设置为指向函数内的新内存(不返回它)IN C

matlab - 如何在 Matlab 中计算两个矩阵减去公共(public)矩阵的外积的平方和?

c++ - C++ 中缺少前向声明的问题

C++ Push_back 一个对象并获取指向该对象的指针

c++ - 读取存储在 double 中的 8 字节字段的 4 个字节

使用 gsl 的复杂矩阵乘法

cublas 矩阵乘法不符合预期

c++ - C和C++对内存的误解