如何将多维数组作为参数传递?

时间:2019-07-08 09:46:36

标签: c arrays multidimensional-array

#include <stdio.h>

void input(int* r1, int* r2, int* c1, int* c2,
  int arr1[*r1][*c1], int arr2[*r2][*c2]);
void matrix_multiply(int* r1, int* r2, int* c1, int* c2, 
  int arr1[*r1][*c1], int arr2[*r2][*c2], int result[50][50]);
int ouput_matrix(int*r1 , int* c2, int result[50][50]);

int main(void)
{
    int arr1_out[50][50];
    int arr2_out[50][50];
    int result_out[50][50];

    int a = 0;
    int* r1 = &a;

    int b = 0;
    int* r2 = &b;

    int c = 0;
    int* c1 = &c;

    int d = 0;
    int* c2 = &d;

    input(r1,r2,c1,c2,arr1_out,arr2_out);
    matrix_multiply(r1, r2,c1, c2,arr1_out, arr2_out,result_out);

    output_matrix(r1,c2,result_out);

}

void input(int* r1,int* r2,int* c1,int* c2,int arr1[*r1][*c1],int arr2[*r2][*c2])
{
    printf("Enter the no. of rows and columns of first matrix ");
    scanf("%d %d\n",r1,c1);

    printf("Enter the no. of rows and columns of second matrix");
    scanf("%d %d\n",r2,c2);

    while(*c1 != *r2)
    {
        printf("Matrix cannot be multiplied...no. of columns of 1st matrix not equal to no. of rows of 2nd matrix");

        printf("Enter the no. of rows and columns of first matrix ");
        scanf("%d %d\n",r1,c1);

        printf("Enter the no. of rows and columns of second matrix");
        scanf("%d %d\n",r2,c2);
    }

    printf("Enter the elements of matrix 1");
    for(int i = 0;i < *r1;i ++)
        for(int j = 0;j < *c1;j ++)
        {
            printf("Enter element a%d%d",i + 1,j + 1);
            scanf("%d",&arr1[i][j]);
        }


    printf("Enter the elements of matrix 2");
    for(int i = 0;i < *r2;i ++)
        for(int j = 0;j < *c2;j ++)
        {
            printf("Enter element b%d%d",i + 1,j + 1);
            scanf("%d",&arr2[i][j]);
        }
}

void matrix_multiply(int* r1,int* r2,int* c1,int* c2,int arr1[*r1][*c1],int 
arr2[*r2][*c2],int result[50][50])
{
    for(int i = 0; i < *r1; ++i)
        for(int j = 0; j < *c2; ++j)
        {
            result[i][j] = 0;
        }

    for(int i = 0; i < *r1; ++i)
        for(int j=0; j < *c2; ++j)
            for(int k=0; k < *c1; ++k)
            {
                result[i][j]+=arr1[i][k]*arr2[k][j];
            }
}

int ouput_matrix(int*r1 , int* c2, int result[50][50])
{
    printf("\nOutput Matrix:\n");
    for(int i = 0; i < *r1; ++i)
        for(int j = 0; j < *c2; ++j)
        {
            printf("%d  ", result[i][j]);
            if(j == *c2-1)
                printf("\n\n");
        }
    return 0;
}

错误:

matrix_multi.c:32:53: runtime error: variable length array bound evaluates to non-positive value 0
matrix_multi.c:32:58: runtime error: variable length array bound evaluates to non-positive value 0
matrix_multi.c:32:72: runtime error: variable length array bound evaluates to non-positive value 0
matrix_multi.c:32:77: runtime error: variable length array bound evaluates to non-positive value 0

2 个答案:

答案 0 :(得分:1)

您定义了以可变长度数组作为参数的函数。

void input(int* r1,int* r2,int* c1,int* c2,int arr1[*r1][*c1],int arr2[*r2][*c2]);
void matrix_multiply(int* r1,int* r2,int* c1,int* c2,int arr1[*r1][*c1],int 
arr2[*r2][*c2],int result[50][50]);

但是您要传递零值作为数组的大小。

int a = 0;
int* r1 = &a;

int b = 0;
int* r2 = &b;

int c = 0;
int* c1 = &c;

int d = 0;
int* c2 = &d;

可变长度数组的大小不能为零。

您至少应该写

int a = 50;
int* r1 = &a;

int b = 50;
int* r2 = &b;

int c = 50;
int* c1 = &c;

int d = 50;
int* c2 = &d;

但是,由于函数中的用户指定行和列的数量以及数组实际上具有固定的大小,因此您应该将函数声明为

void input(int* r1, int* r2, int* c1, int* c2,
  int arr1[][50], int arr2[][50]);
void matrix_multiply(int* r1, int* r2, int* c1, int* c2, 
  int arr1[][50], int arr2[][50], int result[][50]);

答案 1 :(得分:0)

考虑发布的输入功能

void input(int* r1,int* r2,int* c1,int* c2,int arr1[*r1][*c1],int arr2[*r2][*c2]) {/* */}

两个数组的行数和列数均作为指针传递,目的是在函数内部修改其值,但在函数外部将它们初始化为零,以取消对VLA参数,实际上是两个零大小的数组。无法做到这一点。

一种可行的方法是先询问用户尺寸,检查尺寸是否有效,然后声明数组并将其传递给各个函数。类似于以下内容。

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

typedef int value_t;

int input_matrix( const char *format, size_t rows, size_t cols, value_t mat[rows][cols] );
void print_matrix( const char *format, size_t rows, size_t cols, value_t mat[rows][cols] );

int ask_matrix_dimensions( const char *msg, int *rows, int *cols );

void multiply_matrices( size_t rows, size_t inner, size_t cols,
                        value_t mat_a[rows][inner], value_t mat_b[inner][cols],
                        value_t mult[rows][cols] );

int main(void)
{
    int ra, ca;
    if ( ask_matrix_dimensions("the first matrix", &ra, &ca) != 2 )
        return EXIT_FAILURE;

    int rb, cb;
    if ( ask_matrix_dimensions("the second matrix", &rb, &cb) != 2 )
        return EXIT_FAILURE;

    if ( ca != rb )
    {
        fputs("Error: Wrong dimensions, the two matrices cannot be multiplied.", stderr);   
        return EXIT_FAILURE;
    }
    value_t mat_a[ra][ca];
    if ( input_matrix("%d", ra, ca, mat_a) == EOF )
        return EXIT_FAILURE;

    value_t mat_b[rb][cb];
    if ( input_matrix("%d", rb, cb, mat_b) == EOF )
        return EXIT_FAILURE;

    puts("\nMatrix a:");
    print_matrix("%3d", ra, ca, mat_a);
    puts("Matrix b:");
    print_matrix("%3d", rb, cb, mat_b);

    value_t mat_c[ra][cb];
    multiply_matrices(ra, ca, cb, mat_a, mat_b, mat_c);
    puts("Matrix b:");
    print_matrix("%4d", ra, cb, mat_c);

    return EXIT_SUCCESS;
}

int input_matrix( const char *format, size_t rows, size_t cols, value_t mat[rows][cols] )
{ 
    int ret;
    for(size_t r = 0; r < rows; ++r)
    {
        for(size_t c = 0; c < cols; ++c)
        {
            printf("Enter element (%zu, %zu): ", r + 1, c + 1);
            while ( (ret = scanf(format, &mat[r][c])) != 1 )
            {
                if (ret == EOF)
                {
                    fputs("Error: Unexpected end of input.", stderr);
                    return ret;
                }
                puts("Please enter a valid number.");
                for (int ch = getchar(); ch != EOF  &&  ch != '\n'; ch = getchar());
            }
        }
    }
    return 0;
}

void print_matrix( const char *format, size_t rows, size_t cols, value_t mat[rows][cols] )
{
    for (size_t r = 0; r < rows; ++r)
    {
        for (size_t c = 0; c < cols; ++c)
        {
            printf(format, mat[r][c]);
        }
        putchar('\n');
    }
}

int ask_matrix_dimensions( const char *msg, int *rows, int *cols )
{
    printf("Please enter the number of rows and columns of %s: ", msg);
    int ret;
    while ( (ret = scanf("%d%d", rows, cols)) != 2  ||  *rows < 1  ||  *cols < 1)
    {
        if (ret == EOF)
        {
            fputs("Error: Unexpected end of input.", stderr);
            break;
        }
        puts("Please enter two positive numbers.");
        for (int ch = getchar(); ch != EOF  &&  ch != '\n'; ch = getchar());
    }
    return ret;
}

void multiply_matrices( size_t rows, size_t inner, size_t cols,
                        value_t mat_a[rows][inner], value_t mat_b[inner][cols],
                        value_t mult[rows][cols] )
{
    for (size_t i = 0; i < rows; ++i)
    {
        for (size_t j = 0; j < cols; ++j)
        {
            mult[i][j] = mat_a[i][0] * mat_b[0][j];
        }
        for (size_t k = 1; k < inner; ++k)
        {
            for (size_t j = 0; j < cols; ++j)
            {
                mult[i][j] += mat_a[i][k] * mat_b[k][j];
            }   
        }        
    }
}