Whats wrong with the c code?

流过昼夜 提交于 2019-12-25 19:42:14

问题


#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.


回答1:


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:


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



回答3:


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


来源:https://stackoverflow.com/questions/29925206/whats-wrong-with-the-c-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!