使用双指针函数 C 语言访问二维数组
Accessing a 2D array using double pointer to function C language
我试图在双指针的帮助下从 C 函数访问的二维数组中找到所有值的最大值。
当我 运行 代码时,它只是终止并向调用函数返回任何值。
我尝试更改代码以打印所有值以查找问题并发现它仅打印 1 和 2 用于以下示例数据作为输入。
对于示例代码 运行,我提供了 row=2、col=2 和 values=1,2,3,4
请告诉我为什么?另外,如果您的问题不清楚,请这样说。我度过了艰难的一天,所以也许无法解释得更好。
代码有一些限制:
1. 函数签名(int **a,int m,int n)
#include<stdio.h>
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;j<n;j++){
if(a[i][j]>max){
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;
}
There is a few restrictions to the code: 1. Function Signature(int **a,int m,int n)
因此我猜你的任务是使用一个指针数组,所有指针都指向分配?
#include<stdio.h>
#include<stdlib.h>
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; j<n; j++)
{
if(a[i][j]>max)
{
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<row;i++)
{
arr[i] = malloc(col * sizeof(int));
if (!arr[i])
{
printf("arr[%d] not malloc'd\n", i);
abort();
}
for(j=0;j<col;j++)
{
arr[i][j] = 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");
}
printf("\nThe maximum element in the matrix is %d",findMax(arr, row, col));
return 0;
}
在单个 malloc 中完成它的任务留给 reader。
作为练习
我试图在双指针的帮助下从 C 函数访问的二维数组中找到所有值的最大值。 当我 运行 代码时,它只是终止并向调用函数返回任何值。
我尝试更改代码以打印所有值以查找问题并发现它仅打印 1 和 2 用于以下示例数据作为输入。 对于示例代码 运行,我提供了 row=2、col=2 和 values=1,2,3,4
请告诉我为什么?另外,如果您的问题不清楚,请这样说。我度过了艰难的一天,所以也许无法解释得更好。
代码有一些限制: 1. 函数签名(int **a,int m,int n)
#include<stdio.h>
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;j<n;j++){
if(a[i][j]>max){
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;
}
There is a few restrictions to the code: 1. Function Signature(int **a,int m,int n)
因此我猜你的任务是使用一个指针数组,所有指针都指向分配?
#include<stdio.h>
#include<stdlib.h>
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; j<n; j++)
{
if(a[i][j]>max)
{
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<row;i++)
{
arr[i] = malloc(col * sizeof(int));
if (!arr[i])
{
printf("arr[%d] not malloc'd\n", i);
abort();
}
for(j=0;j<col;j++)
{
arr[i][j] = 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");
}
printf("\nThe maximum element in the matrix is %d",findMax(arr, row, col));
return 0;
}
在单个 malloc 中完成它的任务留给 reader。
作为练习