0

There is a warning showing incompatible pointer conversion and a note is also there in line 4 "expected char** but argument is of type char *[2]".

But why please help and explain. And is this problem can be solved by dynamic memory allocation?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
char** cavityMap(int grid_count, char **grid) {
  int i, j, max = 0;
  char y = 'X';
  if (grid_count == 1) {
    return grid;
  } else {
    for (i = 0; i < grid_count; i++)
      for (j = 0; j < grid_count; j++) {
        if (max < (int) grid[i][j]) {
          max = grid[i][j];
        }
      }
    for (i = 0; i < grid_count; i++)
      for (j = 0; j < grid_count; j++) {
        if (i != 0 && i != grid_count - 1 && j != 0 && j != grid_count - 1) {
          if (grid[i][j] == (char) max)
            grid[i][j] = y;
        }

      }

    return grid;
  }
  return 0;
}

int main() {
  int n = 2, i, j;
  char **result;
  char s[2][2];
  s[0][0] = '1';
  s[0][1] = '7';
  s[1][0] = '6';
  s[1][1] = '8';
  result = cavityMap(n, s);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      printf("%c", result[i][j]);
    }
    printf("\n");
  }
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Rishabh
  • 11
  • 3
  • 4
    `char s[2][2]` does not convert to `char **` when passed as a function argument; It converts to `char (*)[2]`. Anyone that told you the two are synonymous was mistaken. – WhozCraig Sep 17 '18 at 20:55
  • so how can i pass this 2d array without using dynamic memory allocation . Help is appreciated. – Rishabh Sep 17 '18 at 21:03
  • @Rishabh just pass a `char *` (pointer to beginning of the matrix) and use that to access all the elements (e.g. `*(grid + i*grid_count + j) = some_int; //same thing as grid[i][j] = some_int;`) – bigwillydos Sep 17 '18 at 21:22
  • Code could use something like `char (*cavityMap(int grid_count, char grid[][2])) [2] { ... }` and `char **result;` --> `char (*result)[2];`, yet that is a bit ugly. – chux - Reinstate Monica Sep 17 '18 at 21:29
  • If your C toolchain supports [VLAs](https://en.wikipedia.org/wiki/Variable-length_array), you could use those. Most reasonable C implementations do; some (ex. MS) don't. – WhozCraig Sep 17 '18 at 21:39
  • @Sir Chux can i write char grid[2][2] instead of char **grid. – Rishabh Sep 17 '18 at 22:36

1 Answers1

3

As others have said char **a is NOT the same as char a[][]

But if you do have a char a[n][n] you can send it as a parameter to a function as long as you also tell the function what n is. Like this

#include <stdio.h>

void
process_array(int n, char a[n][n])
{
  for (int i=0; i < n; i++)
    {
      for (int j=0; j < n; j++)
        printf("%c", a[i][j]);
    }
}

int
main()
{
  char a[2][2] = {{'h', 'o'}, {'l', 'a'}};

  process_array(2, a);

  printf("\n");
}

Oh, and by the way, if your array is not square, you should be able to do

void
process_array(int n, int m, char a[n][m])

The point here is that when declaring arrays as function parameter in modern c, the array dimensions does not have to be constants like this:

void
process_array(char a[2][2])

Just remember that arrays are always passed as pointers in function parameters, so doing sizeof(a) in process_array will return the size of a pointer

HAL9000
  • 2,138
  • 1
  • 9
  • 20