-3
#include<stdio.h>
int findMax(int **a,int m,int n)
{
  int max,i,j;
  max=a[0][0];
  for(i=0;i<m;i++)
  for(j=0;j<n;j++)
  if(max<a[i][j])
    max=a[i][j];
  return max;
}
int main()
{
  int a[20][20],m,n,i,j,maxim;
  scanf("%d",&m); //Rows
  scanf("%d",&n); //Cols
  for(i=0;i<m;i++)
   for(j=0;j<n;j++)
     scanf("%d",&a[i][j]);
  maxim=findMax((int **)a,m,n);
 printf("Max is %d\n",maxim);
 return 0;
}

The above code must give the maximum element in the input matrix.

PROBLEM
When the code is compiled, i'm not getting any error or warning, But DURING EXECUTION the code just stops running after taking the input.!

The problem statement says that int findMax(int **a,int m,int n) has to be used.

Hasan 0
  • 29
  • 7
  • 2
    Run in a debugger to locate where the crash happens. – Some programmer dude Apr 28 '15 at 16:45
  • 2
    But the problem is most likely you trying to use an array of arrays as a pointer to pointer, which is incorrect. See [this old answer of mine](http://stackoverflow.com/a/18440456/440558) for an explanation of why. I suggest you read about allocating memory dynamically. – Some programmer dude Apr 28 '15 at 16:46
  • In man you declare `int a[20][20]` and then fill in a corner of it, but you pass it into findMax as `int **a`. findMax has no way to know where to find the data you entered...it has to know the true dimensions of the array as well as the size of the patch you put numbers into. There are probably other errors, but that's a big one. – Lee Daniel Crocker Apr 28 '15 at 16:48

3 Answers3

4

A 2D array does not decay to a pointer to a pointer. By using:

  maxim=findMax((int **)a,m,n);

you are forcing the compiler to ignore your error.

Instead of

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

use

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

and then, call the function simply using:

  maxim=findMax(a,m,n);

You said:

The problem statement says that int findMax(int **a,int m,int n) has to be used.

In that case, you have cannot use a 2D array for a. You'll have to use:

int main()
{
   // Define a to be an array of pointers.
   int* a[20];
   int m,n,i,j,maxim;

   scanf("%d",&m); //Rows

   // Make sure m not greater than 20. Otherwise, you'll end up
   // accessing memory out of bounds.
   if ( m > 20 )
   {
      // Deal with error.
   }

   scanf("%d",&n); //Cols

   for(i=0;i<m;i++)
   {
      a[i] = malloc(sizeof(int)*n);
      for(j=0;j<n;j++)
      {
         scanf("%d",&a[i][j]);
      }
   }

   maxim=findMax(a,m,n);
   printf("Max is %d\n",maxim);

   // Deallocate memory.
   for(i=0;i<m;i++)
   {
      free(a[i]);
   }

   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1
int findMax(int **a,int m,int n)

should be

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

Check the below code. By making a a pointer to pointer you are good with your API's prototype

int main()
{
  int **a,m,n,i,j,maxim;
  scanf("%d",&m); //Rows
  scanf("%d",&n); //Cols
  a =  malloc(sizeof(int *) * m);
  for(i=0;i<m;i++)
  {
    a[i] = malloc(sizeof(int) * n);
   for(j=0;j<n;j++)
     scanf("%d",&a[i][j]);
  }
  maxim=findMax((int **)a,m,n);
 printf("Max is %d\n",maxim);
 return 0;
}
Gopi
  • 19,784
  • 4
  • 24
  • 36
1

Instead of int a[20][20], use int **a and allocate its space.

int a1[20][20] declares a1 as array 20 of array 20 of int.
int **a2 declares a2 as pointer to pointer to int.
@Ref

a1 is 2D array of int.
a2 is array of pointers. a2[i] points to an int (or the first in an array of int).
These two types are similar, but not compatible.

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

int main(void) {
  int m, n, i, j, maxim;
  int **a;

  scanf("%d", &m); //Rows
  scanf("%d", &n); //Cols
  a = calloc(m, sizeof *a);
  if (a == NULL)
    return EXIT_FAILURE;
  for (i = 0; i < m; i++) {
    a[i] = calloc(n, sizeof *a[i]);
    if (a[i] == NULL)
      return EXIT_FAILURE;
    for (j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
    }
  }

  maxim = findMax(a, m, n);
  printf("Max is %d\n", maxim);

  for (i = 0; i < m; i++) {
    free(a[i]);
  }
  free(a);
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256