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.
matrix = variable(input1, input2); where 'matrix' is the output and 'variable' is the function name. 'input1' and 'input2' are also matrices of order 32x32.
A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name.
Explanation: When an array is passed as parameter to a function then the function can change values in the original array.
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);
}
}
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With