如何在C中将2D数组作为参数传递?

时间:2015-04-17 07:56:19

标签: c arrays

以下代码中永远不会输入功能部分 请帮帮我!

#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;
}

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

3 个答案:

答案 0 :(得分:0)

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;
}

如果您事先不知道2d数组的大小,可以使用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;
}

答案 1 :(得分:0)

你在这里做什么: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是一个关于这个主题的简单教程。

答案 2 :(得分:0)

#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;
}