繁体   English   中英

c - 如何将二维数组作为双指针传递给c中的函数?

[英]How to pass a 2d array as a double pointer to a function in c?

我正在尝试将二维数组作为指向函数的双指针发送,但它让我一直显示此错误

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 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM