使用指向函数C语言的双指针访问2D数组

我试图在双指针的帮助下找到从C函数访问的2D数组中的所有值的最大值。 当我运行代码时,它会终止并返回任何值给调用者函数。

我试图更改代码以打印所有值以找出问题,并发现它只打印1和2作为输入以下示例数据。 对于示例代码运行,我提供了row = 2,col = 2和values = 1,2,3,4

请让我知道为什么? 如果你的问题不清楚,请说出来。 我度过了艰难的一天所以也许无法解释得更好。

代码有一些限制:1。函数签名(int ** a,int m,int n)

#include int findMax(int **a,int m,int n){ int i,j; int max=a[0][0]; for(i=0;i<m;i++){ for(j=0;jmax){ max=a[i][j]; } //printf("\n%d",a[i][j]); } } return max; } int main(){ int arr[10][10],i,j,row,col; printf("Enter the number of rows in the matrix"); scanf("%d",&row); printf("\nEnter the number of columns in the matrix"); scanf("%d",&col); printf("\nEnter the elements of the matrix"); for(i=0;i<row;i++){ for(j=0;j<col;j++){ scanf("%d",&arr[i][j]); } } printf("\nThe matrix is\n"); for(i=0;i<row;i++){ for(j=0;j<col;j++){ printf("%d ",arr[i][j]); } printf("\n"); } int *ptr1 = (int *)arr; printf("\nThe maximum element in the matrix is %d",findMax(&ptr1,row,col)); return 0; } 

代码有一些限制:1。函数签名(int ** a,int m,int n)

我猜你的任务是使用一个指针数组,所有这些都指向分配?

 #include #include int findMax(int **a,int m,int n){ int i,j; int max=a[0][0]; for(i=0; imax) { max=a[i][j]; } //printf("\n%d",a[i][j]); } } return max; } int main(){ int **arr; int i,j,row,col; printf("Enter the number of rows in the matrix"); scanf("%d",&row); printf("\nEnter the number of columns in the matrix"); scanf("%d",&col); arr = malloc(row * sizeof(int*)); if (!arr) { printf("arr not malloc'd\n"); abort(); } for(i=0;i 

在单个malloc中执行此任务的任务留给读者练习。