0

I'm trying to send a 2d array as a double pointer to a function but it keeps me showing this error

matp.c:15:7: warning: passing argument 1 of ‘read’ from incompatible pointer type [-Wincompatible-pointer-types]
  read(a,r,c);
matp.c:3:6: note: expected ‘int **’ but argument is of type ‘int (*)[(sizetype)(c)]’
 void read(int **,int,int);
  ^~~~

here is the code

void read(int **,int,int);
void disp(int **,int,int);
int main()
{
int r,c;

int a[r][c];
printf("\nEnter the elements of the matrix:\n");
read(a,r,c);

printf("\nThe entered matrix is:\n");
disp(a,r,c);
printf("\nThe transpose of the given matrix is :\n");
disp(a,c,r);

return 0;
}

void disp(int **a,int r,int c)
{
int i,j;
for(i=0;i<=r;i++)
{
    for(j=0;j<=c;j++)
    {
        printf("%d",*(j+*(&(a)+i)));
    }
}
return;
}

I tried to read a matrix and print it's transpose

  • 1
    Check this [Link](https://stackoverflow.com/questions/16724368/how-to-pass-a-2d-array-by-pointer-in-c) – Blue Dice Dec 22 '19 at 07:34

1 Answers1

0

The C compiler doesn't know the size, and therefore doesn't know how to deal with it.

Use:

printf("%d",*( (int*) a + i * c + j )));

And when calling convert:

disp((int**) a, r, c);

And be careful when printing the transposed matrix. Just changing the size is not going to give you what you want. You can print the transposed matrix like this:

void disp_transposed(int **a,int r,int c)
{
    int i,j;
    for(j=0;j<c;j++)
    {
        for(i=0;i<r;i++)
        {
            printf("%d",*( (int*) a + i * c + j )));
        }
    }
    return;
}

Also, using <=r and <=c will get you out of the bounds of your matrix(when i == r and/or j == c).

Dinu
  • 129
  • 2
  • 6