我正在尝试将2d数组作为指向函数的双指针发送,但它使我一直显示此错误
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);
^~~~
这里是代码
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;
}
我试图读取矩阵并打印它的转置
C编译器不知道大小,因此不知道如何处理它。
用途:
printf("%d",*( (int*) a + i * c + j )));
并且在调用转换时:
disp((int**) a, r, c);
并且在打印转置矩阵时要小心。只是改变大小并不能满足您的需求。您可以像这样打印转置矩阵:
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;
}
此外,使用<=r
和<=c
也会使您脱离矩阵的边界(当i == r
和/或j == c
时。)>