C:将矩阵传递给函数的所有可能方法以及有关 argc 和 argv 的一些信息

问题描述

这几天一直被多个编译错误卡住,在底部,我最大的错误一个,你可以在下面的代码中看到。

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

void generateMatrix(int rows,int cols,int matr[][]);
void show(int rows,int matr[][]);
// void analize(int matr[rows][cols],int rows,int cols);

int main(int argc,char* argv[])
{
    srand(time(NULL));
    if(argc < 3)
    {
        printf("Not enough inputed arguments,you have to insert only two number,one for rows the last for cols\n");
        return 1;
    }
    else
    {
        int rows = atoi(argv[1]),cols = atoi(argv[2]);
        int mat[rows][cols];
        generateMatrix(rows,cols,mat);
        show(rows,mat);
        // analize(mat,rows,cols);
        
        return 0;
    }
}

void generateMatrix(int rows,int matr[][])
{
    for(int i=0; i<rows; i++)
        for(int j=0; j<cols; j++)
            matr[i][j] = rand() % 20;
}

void show(int rows,int matr[][])
{
    for(int i=0; i<rows; i++)
    {
        for(int j=0; j<cols; j++)
            printf("%5d",matr[i][j]);
        printf("\n");
    }
}

这里是编译结果:

t.c:5:45: error: array type has incomplete element type ‘int[]’
    5 | void generateMatrix(int rows,int matr[][]);
      |                                             ^~~~
t.c:5:45: note: declaration of ‘matr’ as multidimensional array must have bounds for all dimensions except the first
t.c:6:35: error: array type has incomplete element type ‘int[]’
    6 | void show(int rows,int matr[][]);
      |                                   ^~~~
t.c:6:35: note: declaration of ‘matr’ as multidimensional array must have bounds for all dimensions except the first
t.c: In function ‘main’:
t.c:21:36: error: type of formal parameter 3 is incomplete
   21 |         generateMatrix(rows,mat);
      |                                    ^~~
t.c:22:26: error: type of formal parameter 3 is incomplete
   22 |         show(rows,mat);
      |                          ^~~
t.c: At top level:
t.c:29:45: error: array type has incomplete element type ‘int[]’
   29 | void generateMatrix(int rows,int matr[][])
      |                                             ^~~~
t.c:29:45: note: declaration of ‘matr’ as multidimensional array must have bounds for all dimensions except the first
t.c:36:35: error: array type has incomplete element type ‘int[]’
   36 | void show(int rows,int matr[][])
      |                                   ^~~~
t.c:36:35: note: declaration of ‘matr’ as multidimensional array must have bounds for all dimensions except the first

现在问题来了:

  1. 修复错误需要什么?
  2. 是否可以改进此代码中的某些内容,而无需进行太多更改?? (可选)
  3. 在这种情况下,矩阵将用作指针吗? (工作保存数据并正确传递给函数?)

感谢您花费的时间和未来的答案。

解决方法

编译器无法将 int matr[][] 作为函数参数处理。

解决此问题的最简单方法是将其作为 int *matr 传递。您可以按如下方式取消引用它:matr[i * cols + j]。由于行优先排序,这是以正确的顺序访问它。要抑制编译器警告,您需要通过键入 &matr[0][0] 来传递它。大多数程序员不会依赖多维数组的定义顺序,而是继续将其声明为单维。

const 是你说某些不能通过引用写入的方式。所以是的,您可以将变量声明为 const。它不会破坏任何东西。请注意,您必须在定义 const 时为其分配它,这正是您想要执行的操作。

,

您遗漏了分配或传递可变维度矩阵的最简单方法 - 使用 a VLA (variable-length array)

// return a pointer to a VLA
void *allocateArray( size_t x,size_t y )
{
    int (*array)[ x ][ y ] = malloc( sizeof( *array ) );

    return( array );
}

请注意,返回类型是 void *,因此没有类型安全。

这将以更类型安全的方式分配数组

void allocateArray( size_t x,size_t y,int ( **array )[x][y] )
{
    *array = malloc( sizeof( **array ) );

    return( array );
}

你可以用这个表格来处理一个矩阵:

void printMatrix( size_t x,int array[x][y] )
{
    for ( size_t ii = 0; ii < x; ii++ )
    {
        char *separator = "";
        for ( size_t jj = 0; jj < y; jj++ )
        {
            printf( "%s%d",sep,array[ ii ][ jj ] );
            separator = ",";
        }
        printf( "\n" );
    }
}

VLA 确实需要符合 C99 的编译器。在 C11 中,VLA 是可选的,但不支持 VLA 的符合 C11 的编译器必须定义 __STDC_NO_VLA__