使用双指针访问2D数组以使用C语言 [英] Accessing a 2D array using double pointer to function C language

查看:85
本文介绍了使用双指针访问2D数组以使用C语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图借助双指针在从C函数访问的2D数组中查找所有值的最大值. 当我运行代码时,它以向调用方函数返回任何值而终止.

I am trying to find the maximum of all values in a 2D array accessed from a C function with the help of double pointer. When I run the code it just terminates with returning any value to the caller function.

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

I tried to change the code to print all values to find out the problem and found that it only prints 1 and 2 for the following sample data as input. For a sample code run, I provided row=2, col=2 and values=1,2,3,4

请让我知道为什么?另外,如果您的问题不清楚,请这样说.我度过了艰难的一天,所以也许无法解释得更好.

Please let me know why? Also if you question is not clear please say so. I've had a tough day so maybe couldn't explain better.

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

There is a few restrictions to the code: 1. Function Signature(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;
}

推荐答案

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

There is a few restrictions to the code: 1. Function Signature(int **a,int m,int n)

我想您的任务因此是使用指针数组,所有这些指针都指向分配吗?

I guess your task is therefore to use an array of pointers, all of which point to allocations?

#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中完成任务的工作留给读者练习.

The task of doing it in a single malloc is left as an exercise to the reader.

这篇关于使用双指针访问2D数组以使用C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆