使用函数的矩阵乘法中的警告

时间:2016-02-03 16:19:02

标签: c pointers matrix-multiplication

我有三个错误:

  1. 赋值从指针生成整数而不使用强制转换
  2. 从不兼容的指针类型
  3. 传递read的参数1
  4. 预期int * (*)[10]但参数类型为int (*)[10][10]
  5. 以下是代码:

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

1 个答案:

答案 0 :(得分:1)

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

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

您不应将函数命名为read,因为它是C库用于实现FILE*流接口的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

矩阵乘法的原型应该改为:

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