C语言 将矩阵作为函数中的参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18661702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:23:02  来源:igfitidea点击:

Passing matrix as a parameter in function

cmatrix

提问by user2714823

I have been trying to write a function which gives the index of rows and columns whoses element is 0. I tried using a function

我一直在尝试编写一个函数,它给出元素为 0 的行和列的索引。我尝试使用一个函数

void make_zero(int matrix[][],int row,int col)
{
    int row, col;
    int i,j;  

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

    }
}

But at the time of compiling it gives an error"error: array type has incomplete element type". I also tried declaring the matrix globally and giving it a pointer. But it doesn't work for me. Help me out with this how can we pass matrix to a function in C.

但是在编译时它给出了一个错误“错误:数组类型具有不完整的元素类型”。我还尝试全局声明矩阵并给它一个指针。但这对我不起作用。帮我解决这个问题,我们如何将矩阵传递给 C 中的函数。

采纳答案by haccks

Try this

尝试这个

  void make_zero(int row, int col, int matrix[row][col])
  {
     int i,j;  
     for(i=0;i<row;i++)
        for(j=0;j<col;j++)
        {
            if(matrix[i][j]==0)
               printf("%d %d\n",i,j);
        }

  }

回答by Sergey L.

Your problem is that multi-dimensional arrays in C need to have their lengths known except for the outermost value.

你的问题是 C 中的多维数组需要知道它们的长度,除了最外面的值。

What you can do is pass the pointer to the memory holding the matrix and then cast it to the right type:

您可以做的是将指针传递给保存矩阵的内存,然后将其转换为正确的类型:

void make_zero(void* _matrix,int row,int col)
{
    int i,j;  
    int (*matrix)[col] = _matrix;
    for(i=0;i<row;i++)
        for(j=0;j<col;j++)
        {
             if(matrix[i][j]==0){
                   printf("%d %d\n",i,j);
        }

    }
}

回答by Gangadhar

Assuming if you declare int matrix[10][10];like this

假设如果你声明int matrix[10][10];这样

make_zero(matrix,5,5); //function call 

void make_zero(int mat[][10],int row,int col) //definition   
{
//statements
}   

EDIT:

编辑:

the above solution works as long as the actual array passed always has a second dimension of 10 You can use like this As @Jonathan Leffler suggested

只要传递的实际数组的第二维始终为 10,上述解决方案就可以工作,您可以像这样使用@Jonathan Leffler 建议

 make_zero(5,5,matrix); //function call 
 void make_zero(int row, int col, int matrix[row][col]) //definition 
     {
     //statements
     }

回答by 42n4

Matrix as argument to a function in C

矩阵作为 C 中函数的参数

I have made an "ultimate" solution to this problem in gcc C11/C99 using these links:

我使用以下链接在 gcc C11/C99 中针对此问题提出了“终极”解决方案:

http://c-faq.com/aryptr/dynmuldimary.html

http://c-faq.com/aryptr/dynmuldimary.html

http://c-faq.com/aryptr/ary2dfunc3.html

http://c-faq.com/aryptr/ary2dfunc3.html