[英]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.