Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

How to pass a 2D array as a parameter in C?

The section of function is never entered in the following code please help me out!

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

//This function correct it if possible

int main()
{
   int m,n,a[50][50],i,j,k=0;
   printf("Enter the number of rows in the matrix
");
   scanf("%d",&m);
   printf("Enter the number of columns in the matrix
"); 
  scanf("%d",&n);
  printf("Enter the elements in the matrix
");
  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("
");
     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("
The maximum element in the matrix is %d",k);
  return 0;
}

please help me out!! Thanks to you in advance!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

what you are doing here: int findMax(int *a[], int m, int n) is trying to point at int 50 times, but you want to point at a variabile type int 50 times. therefor you want to decleare the pointer before you decleare the array.

use therefor:

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

this should work now without the casting

just turn it into:

k=findmax(a,m,n);

here is a simple tutorial on the subject.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...