1

I have this function

int findMax(int **a,int row,int column)

and i am having 2-d array which is a[10][10].

Now what should i do so i can pass the address of this array to my function pointer that is **a ?

i saw other answers but the problem remain the same i have to pass the address but whenever i am trying to do this my compiler starts giving error and says cannot convert int to int and type mismatch error. And i have to use that function only which has **a.

Give me your valuable suggestions and idea guys how should i do this?

Here is my code.

  #include<stdio.h>

  int findMax(int **a,int r,int c);

 int main()
 {
   int a[10][10],i,j,mx,r,c;


printf("Enter the number of rows in the matrix\n");
 scanf("%d",&r);
 printf("Enter the number of columns in the matrix\n");
 scanf("%d",&c);
printf("Enter the elements in the matrix\n");

for(i=1;i<=r;i++)
{
  for(j=1;j<=c;j++)
        scanf("%d",&a[i][j]);
}
printf("The matrix is\n");
for(i=1;i<=r;i++)
 {
  for(j=1;j<=c;j++)
        printf("%d ",a[i][j]);
  printf("\n");
}
printf("\n");
mx=findMax((*a)[10],r,c);
printf("The maximum elements in the matrix is %d\n",mx);
return 0;
}

int findMax(int **a,int r,int c)
 {
int t,i,j;
t=a[1][1];
for(i=1;i<=r;i++)
 {
  for(j=1;j<=c;j++)
  {
        if(a[i][j]>t)
             t=a[i][j];
  }
  }
return (t);
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
Deepak Kumar
  • 131
  • 2
  • 2
  • 12
  • possible duplicate of [Create a pointer to two-dimensional array](http://stackoverflow.com/questions/1052818/create-a-pointer-to-two-dimensional-array) – Colonel Thirty Two May 29 '15 at 20:44

1 Answers1

0

You can define the function either like

int findMax( int ( *a )[10], int r );

or like

int findMax( int r, int c, int ( *a )[c] );

The last declaration is posiible if your compiler supports C99.

Take into account that the function has typos. Instead of

t=a[1][1];

there has to be

t=a[0][0];

Or these loops

for(i=1;i<=r;i++)
 {
  for(j=1;j<=c;j++)

have to be written like

for(i=0;i < r;i++)
 {
  for(j=0; j < c;j++)

The same problem exists in main. For example instead of

for(i=1;i<=r;i++)
{
  for(j=1;j<=c;j++)
        scanf("%d",&a[i][j]);
}

you have to write

for ( i = 0;i < r; i++ )
{
  for ( j = 0; j < c; j++ )
        scanf("%d",&a[i][j]);
}

because indices in C start from 0.

If your compiler supports C(( then the program can look like

#include <stdio.h>

int findMax( int r, int c, int a[r][c] )
{
    int max = a[0][0];

    for ( int i = 0; i < r; i++ )
    {
        for ( int j = 0; j < c; j++ )
        {
            if ( max < a[i][j] ) max = a[i][j];
        }
    }

    return max;
}   


int main(void) 
{
    int r, c;

    printf( "Enter the number of rows in the matrix: " );
    scanf( "%d", &r );
    printf( "Enter the number of columns in the matrix: " );
    scanf( "%d", &c );

    int a[r][c];

    for ( int i = 0; i < r; i++ )
    {
        for ( int j = 0; j < c; j++ )
        {
            scanf( "%d", &a[i][j] );
        }
    }

    printf( "The matrix is\n" );

    for ( int i = 0; i < r; i++ )
    {
        for ( int j = 0; j < c; j++ ) printf( "%d ", a[i][j] ); 
        printf( "\n" );
    }


    printf( "\n" );

    int mx = findMax( r, c, a );

    printf( "The maximum elements in the matrix is %d\n", mx );

    return 0;
}

Otherwise you can allocate the array dynamically. For example

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

int findMax( int **a, int r, int c )
{
    int i, j;

    int max = a[0][0];

    for ( i = 0; i < r; i++ )
    {
        for ( j = 0; j < c; j++ )
        {
            if ( max < a[i][j] ) max = a[i][j];
        }
    }

    return max;
}   


int main(void) 
{
    int r, c;
    int i, j;
    int **a;
    int mx;

    printf( "Enter the number of rows in the matrix: " );
    scanf( "%d", &r );
    printf( "Enter the number of columns in the matrix: " );
    scanf( "%d", &c );

    a = ( int ** )malloc( r * sizeof( int * ) );

    for ( i = 0; i < r; i++ ) a[i] = ( int * )malloc( c * sizeof( int ) );

    for ( i = 0; i < r; i++ )
    {
        for ( j = 0; j < c; j++ )
        {
            scanf( "%d", &a[i][j] );
        }
    }

    printf( "The matrix is\n" );

    for ( i = 0; i < r; i++ )
    {
        for ( j = 0; j < c; j++ ) printf( "%d ", a[i][j] ); 
        printf( "\n" );
    }


    printf( "\n" );

    mx = findMax( a, r, c );

    printf( "The maximum elements in the matrix is %d\n", mx );

    for ( i = 0; i < r; i++ ) free( a[i] );
    free( a );

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335