【问题标题】:How to pass a 2d array as a double pointer to a function in c?如何将二维数组作为双指针传递给c中的函数?
【发布时间】:2019-12-22 07:05:05
【问题描述】:

我试图发送一个二维数组作为一个函数的双指针,但它让我一直显示这个错误

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 function multidimensional-array function-pointers


【解决方案1】:

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

另外,使用&lt;=r&lt;=c 会让你跳出矩阵的边界(当i == r 和/或j == c 时)。

【讨论】:

    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2014-05-09
    • 2018-12-08
    • 1970-01-01
    相关资源
    最近更新 更多