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;
}
2
solved Whats wrong with the c code?