11

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

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.

haccks
  • 104,019
  • 25
  • 176
  • 264
user2714823
  • 607
  • 5
  • 15
  • 29

4 Answers4

16

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

  }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
haccks
  • 104,019
  • 25
  • 176
  • 264
  • @Jonathan Leffler; What if I use `void make_zero(int col, int matrix[][col])`? – haccks Sep 06 '13 at 16:03
  • 1
    That works as long as the function magically knows how many rows to process. I fixed it so you were not using an uninitialized variable... You should note that this uses C99, VLAs, and you might consider `for (int i = 0; i < row; i++)` notation for the loops. – Jonathan Leffler Sep 06 '13 at 16:04
  • 1
    @JonathanLeffler; I read that: *We must specify the number of columns in a multidimensional array, we don't have to indicate number of rows*. – haccks Sep 06 '13 at 16:08
  • 2
    You don't have to specify the number of rows in the bounds of the array parameter; you do have to know how many rows there are so you don't step out of bounds. That is, you could write: `void make_zero(int col, int matrix[][col], int row)` and then iterate over the number of rows, but it makes for an odd calling sequence. If you have the interface `void make_zero(int col, int matrix[][col]){ int row; ...; for (int i = 0; i < row; i++) ...`, then you are using an uninitialized variable as the bound of your array, which is unlikely to lead to happiness. – Jonathan Leffler Sep 06 '13 at 16:11
  • @JonathanLeffler; I understand what you are trying to say. Thanks a lot. – haccks Sep 06 '13 at 16:15
10

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

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

    }
}
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
3

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

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

 make_zero(5,5,matrix); //function call 
 void make_zero(int row, int col, int matrix[row][col]) //definition 
     {
     //statements
     }
Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • 2
    This works as long as the actual array passed always has a second dimension of 10. It was what was necessary in C89; C99 with VLA (variable-length array) support provides better support for arrays. See the [answer](http://stackoverflow.com/a/18661858/15168) by [haccks](http://stackoverflow.com/users/2455888/haccks) for the C99 version — but note the revised order of the arguments (because you can't use what you have not yet declared). – Jonathan Leffler Sep 06 '13 at 16:07
  • @JonathanLeffler Thank you very much for helpful information. Modified My answer. – Gangadhar Sep 06 '13 at 16:42
1

Matrix as argument to a function in C

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

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

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

Community
  • 1
  • 1
42n4
  • 1,292
  • 22
  • 26