【问题标题】:How to pass a 2D array as a parameter in C?如何在C中将二维数组作为参数传递?
【发布时间】:2015-06-23 23:10:28
【问题描述】:

以下代码中从未输入过函数部分 请帮帮我!

#include<stdio.h>
int findMax(int *a[], int m, int n)//this function is not entering
{
    int i,j,k=0;
    for(i=0;i<m;i++)
      for(j=0;j<n;j++)
        if(k<a[i][j])
           k=a[i][j];
    printf("hi");//this hi is also not printing
                 //this hi is just for testing
    return k;
}

//如果可能,这个函数会更正它

int main()
{
   int m,n,a[50][50],i,j,k=0;
   printf("Enter the number of rows in the matrix\n");
   scanf("%d",&m);
   printf("Enter the number of columns in the matrix\n"); 
  scanf("%d",&n);
  printf("Enter the elements in the matrix\n");
  for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    {
      scanf("%d",&a[i][j]);
    } 
  printf("The matrix is");
  for(i=0;i<m;i++)
  {
     printf("\n");
     for(j=0;j<n;j++)
       printf("%d ",a[i][j]);
  }
  k=findMax((int **)a,m,n);//statements after this is never running but no
                           //compilation errors
  printf("\nThe maximum element in the matrix is %d",k);
  return 0;
}

请帮帮我!! 提前谢谢你!

【问题讨论】:

  • 如果我把mn分别设置为201和403呢?
  • 你用调试器看看会发生什么吗?
  • @SamiKuhmonen 我已经执行过了,它显示的就像编译时错误
  • @SouravGhosh 为什么有人要求 15*15 你去那里?我只需要一个解决方案!
  • hy it was juz asked for 15*15 y do u go there? i just need a solution for this! ... 我只是想提供一些免费的建议来保存您的程序以防错误输入。如果您对接近损坏的代码和just need a solution for this 没问题,那么上帝会帮助您!

标签: c arrays


【解决方案1】:
int findMax(int *a[], int m, int n)

int *a[] 是一个指向 int 的指针数组,你想要一个指向 50 个 int 的数组的指针:

int findMax(int (*a)[50], int m, int n)

int findMax(int a[][50], int m, int n)

你不需要演员,调用它使用:

k = findMax(a, m, n);

或者你可以使用一个平面数组,一个例子:

#include <stdio.h>

void foo(int *arr, int rows, int cols)
{
    int i, j;

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++)
            printf("%d ", arr[i * cols + j]);
        printf("\n");
    }
}

#define ROWS 3
#define COLS 3

int main(void)
{
    int arr[ROWS * COLS];
    int i, j;

    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++)
            arr[i * COLS + j] = i * COLS + j;
    }
    foo(arr, ROWS, COLS);
    return 0;
}

如果您事先不知道二维数组的大小,可以使用malloc

#include <stdio.h>
#include <stdlib.h>

void foo(int *arr, int rows, int cols)
{
    int i, j;

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++)
            printf("%d ", arr[i * cols + j]);
        printf("\n");
    }
}

int main(void)
{
    int i, j, rows, cols;
    int *arr;

    printf("Num of rows:");
    scanf("%d", &rows);
    printf("Num of cols:");
    scanf("%d", &cols);
    arr = malloc(rows * cols);
    if (arr == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++)
            arr[i * cols + j] = i * cols + j;
    }
    foo(arr, rows, cols);
    free(arr);
    return 0;
}

【讨论】:

  • @BLUEPIXY,我不明白你,a[50][50] 的问题在哪里?
  • m 和 n 不仅仅是一直在使用那个部分。
  • @BLUEPIXY,我还是没听懂你
  • 你为什么用foo(3, 2, a);bar(a, 3, 2);,尺寸是50*50
  • foo 旨在体现您的示例(我说的是最后一个)。它没有按预期工作。你记得nm是用户输入的,可能不是50
【解决方案2】:

你在这里做什么:int findMax(int *a[], int m, int n) 试图指向 int 50 次,但你想指向一个变量类型 int 50 次。因此,您希望在清除数组之前清除指针。

为此使用:

int findMax(int (*a)[], int m, int n)

这现在应该可以在没有强制转换的情况下工作

把它变成:

k=findmax(a,m,n);

here 是一个关于这个主题的简单教程。

【讨论】:

  • @BLUEPIXY 我不明白你的意思??你能解释一下吗?
  • int (*a)[] 无效。无法编译。
  • C99g++ 4.9.2 都无法编译。
  • @BLUEPIXY 很奇怪,但希望它能为 Arun 编译
  • 不,二维数组的大小不是确定数组,因为错误信息无效。
【解决方案3】:
#include <stdio.h>
#include <limits.h>

#define MATRIX_SIZE 50

int findMax(int a[MATRIX_SIZE][MATRIX_SIZE], int m, int n){
    //Valid any of the following is
    //int a[MATRIX_SIZE][MATRIX_SIZE]
    //int a[][MATRIX_SIZE]
    //int (*a)[MATRIX_SIZE]
    int i, j, k=INT_MIN;
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            if(k<a[i][j])
                k=a[i][j];
    return k;
}

int main(void){
    int m, n, a[MATRIX_SIZE][MATRIX_SIZE], i, j, k=0;

    printf("Enter the number of rows(<=50) in the matrix\n");
    scanf("%d",&m);
    printf("Enter the number of columns(<=50) in the matrix\n"); 
    scanf("%d",&n);
    if(m < 1 || n < 1 || m > 50 || n > 50){
        printf("invalid input!\n");
        return -1;
    }
    printf("Enter the elements in the matrix\n");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);

    printf("The matrix is");
    for(i=0;i<m;i++){
        printf("\n");
        for(j=0;j<n;j++)
            printf("%d ",a[i][j]);
    }
    k=findMax(a, m, n);
    printf("\nThe maximum element in the matrix is %d\n", k);

    return 0;
}

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 1970-01-01
    • 2023-03-30
    • 2020-10-04
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多