1

I've got this homework. Basically, what I have to do is complete the following code that returns the maximum element of a bidimensional array of 'r' rows and 'n' columns.

#include <stdio.h>

int max_element(int **A, int r, int n) {
// complete the code
int max;
max = a[0][0];
for (int i = 0; i < r; i++) {
    for (int j = 0; j < n; j++) {
        if (A[i][j] > max)
            max = A[i][j];
    }
}
return max; }

// implement a main() function to test the algorithm
int main() {
int A[2][3] = { {1, 0, 4}, {10, 3, 1} };

printf("%d\n", max_element(&A, 2, 3));
return 0; }

I have 1 warning:

  • passing argument 1 of 'max_element' from incompatible pointer type [-Wincompatible-pointer-types]

The console stopped working: a problem caused the program to stop working correctly...

Orion
  • 39
  • 4

2 Answers2

1

Your max_element function is defined as such:

int max_element(int **A, int r, int n);

It takes a pointer to a pointer to int (int**) and you are feeding it this:

int A[2][3];
max_element(&A, 2, 3);

Do you expect the expression &A to yield a result of type int**? It will not. It will in fact yield a result of type int(*)[2][3]. That will not bind to int**. This is where the compiler warning kicks in. Those are incompatible pointers!!

You have a wider problem though. A 2D array is not int**. It has type int[][COLS]. You must specify the second number.

Change your function to be:

const int COLS = 3;

int max_element(int A[][COLS], int r, int n);

and then call as:

max_element(A, 2, 3);
DeiDei
  • 10,205
  • 6
  • 55
  • 80
  • There is nothing wrong with your algorithm, given the changes I described in my answer. Except for a typo in `max = a[0][0]` which should be `max = A[0][0]`. Doesn't your compiler shout at you? – DeiDei Oct 30 '16 at 21:01
0

Change the function prototype of max_element from:

int max_element(int **A, int r, int n)

To

int max_element(int A[][3], int r, int n)

This C-Faq thoroughly explains why. The gist of it is that arrays decay into pointers once, it doesn't happen recursively. An array of arrays decays into a pointer to an array, not into a pointer to a pointer.

And also, you should call max_element by max_element(A, 2, 3) instead of max_element(&A, 2, 3).

If a function is already declared as accepting a pointer to a pointer (as in your case), it is almost certainly meaningless to pass a two-dimensional array directly to it. An intermediate pointer would have to be used when attempting to call it with a two-dimensional array:

int max_element(int **A, int r, int n);

int *ip = &A[0][0];
max_element(&ip, 2, 3);     /* PROBABLY WRONG */

but this usage is misleading and almost certainly incorrect, since the array has been flattened (its shape has been lost).

abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • So if I would use an intermediate pointer it would not work because array of arrays decays only into pointer to array. The error was in the prototype oft he function. But if the function is already declared as pointer to pointer how should I implement the main? Intermediate pointer compile but doesn't work. – Orion Oct 30 '16 at 22:35