-2

There is a problem on leetcode and for C language we have to implement a function with definition as

int numIslands(char** grid, int gridRowSize, int *gridColSizes);

I can not change the definition of this function.

I have implemented it as

#define VISITED 'v'

void dfs(char** grid, int gridRowSize,
         int gridColSizes, int i, int j) {
    grid[i][j] = VISITED; <- throws


    if (i - 1 >= 0 && grid[i - 1][j] == '1') <- throws
        dfs(grid, gridRowSize, gridColSizes, i - 1, j);

    if (i + 1 < gridRowSize && grid[i + 1][j] == '1')
        dfs(grid, gridRowSize, gridColSizes, i + 1, j);

    if (j - 1 >= 0 && grid[i][j - 1] == '1')
        dfs(grid, gridRowSize, gridColSizes, i, j - 1);

    if (j + 1 < gridColSizes && grid[i][j + 1] == '1')
        dfs(grid, gridRowSize, gridColSizes, i, j + 1);
}

int numIslands(char **grid, int gridRowSize, int *gridColSizes) {
    int count = 0;

    for (int i = 0; i < gridRowSize; i++) {
        for (int j = 0; j < *gridColSizes; j++) {
            if (grid[i][j] == '1') {
                count++;
                dfs(grid, gridRowSize, *gridColSizes, i, j);
            }
        }
    }

    return count;
}

int main() {
    char **grid = {
            {'1','1','1','1','0'},
            {'1','1','0','1','0'},
            {'1','1','0','0','0'},
            {'0','0','0','0','0'}
    };
    int cols = 5;

    int res = numIslands(grid, 4, &cols);
}

It compiles, but throws on lines with indexed access to grid (like mentioned lines in code).

therhino
  • 33
  • 2
  • 8
  • 3
    The initializer for `char **grid = { … };` shouldn't be compiling. When you say "this is not working", in what way is it not working? Please provide an MCVE ([MCVE]). It is hard to know what you're doing wrong when you don't show what you're doing. – Jonathan Leffler Apr 12 '19 at 18:14
  • 2
    Possible duplicate of [How to pass 2D array (matrix) in a function in C?](https://stackoverflow.com/questions/3911400/how-to-pass-2d-array-matrix-in-a-function-in-c) – Mark Benningfield Apr 12 '19 at 18:15
  • Please, please, please, pointers are pointers, and arrays are arrays. Each one has its own purpose in life. `int numIslands(ptrdiff_t gridRowSize, ptrdiff_t gridColSize, char grid[gridRowSize][gridColSize]);` is probably what you want – alx - recommends codidact Apr 12 '19 at 18:15
  • And you create the matrix as `char[][] = {...};` (You may need to give the last dimension, I'm not sure). The call to the function is the same. – alx - recommends codidact Apr 12 '19 at 18:17
  • The error you have (you didn't tell) is probably a segfault, and probably because you are writing to what probably is a const variable. – alx - recommends codidact Apr 12 '19 at 18:26
  • Do not ignore the compiler warnings. Your code should have a couple dozen of them from the initialization and another telling you that passing a `char *` into a function expecting `char **` is something you ought not do. Compiler errors tell you that the program's syntax is incorrect and it cannot be parsed and translated into a program. Compiler warnings tell you that while the code is syntactically correct and can be turned into a program, the program likely doesn't do what you expect. – user4581301 Apr 12 '19 at 18:43
  • If you can not change the *declaration* of the function, tell the braindamaged one who wrote it to do it. If it is a tutorial for learning C, as it seems, abandon that tutorial; it will hurt you more than teach you; try another tutorial. – alx - recommends codidact Apr 12 '19 at 18:47
  • I can't get that code to compile with GCC 8.3.0, even with `gcc -std=c90 -c leet59.c` (minimally fussy options; GCC 8 assumes `-std=gnu11` by default). It complains bitterly about the initializer for `char **grid = { … };` as I said before. Which compiler is accepting that code (with the `<- throws` comment put inside comments)? – Jonathan Leffler Apr 12 '19 at 18:56

1 Answers1

0

You might do:

char line1[] = {'1', '1', '1', '1', '0'};
char line2[] = {'1', '1', '0', '1', '0'};
char line3[] = {'1', '1', '0', '0', '0'};
char line4[] = {'0', '0', '0', '0', '0'};
int sizes[] = {
    sizeof(line1) / sizeof(*line1),
    sizeof(line2) / sizeof(*line2),
    sizeof(line3) / sizeof(*line3),
    sizeof(line4) / sizeof(*line4)
};
char* grid[] = {line1, line2, line3, line4};
int gridRowSize = sizeof(grid) / sizeof(*grid);

int res = numIslands(grid, gridRowSize, lines);
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Yes, this works. But is there more elegant solution? Maybe something with simpler one-line initializer like in my code? – therhino Apr 12 '19 at 18:36
  • @therhino not with a function expecting a `char **`. You need to have an array of pointers that point to other arrays containing the data. – user4581301 Apr 12 '19 at 18:40
  • Please, please, please. Pointers are pointers; arrays are arrays. Arrays are accessed by []. The only sane way to calculate the number of members of an array is `#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))`. Then use it like this: `int sizes[] = {ARRAY_SIZE(line1), ARRAY_SIZE(line2), ... };` – alx - recommends codidact Apr 12 '19 at 18:54
  • @CacahueteFrito You can't please everybody. But everybody cannot please me. – user4581301 Apr 12 '19 at 19:14