1

I am fairly new to c++ and prcticing some ds algo problem. From last 24 hours, I have been stuck with this.

/*Find shortest distance of every cell from landmine in a maze*/

#include <iostream>
#include <algorithm>
#include <queue>
#include <climits>

struct cell{
    int x, y;

};

const int M = 6, N = 5;

int row[4] = {-1, 0, 1, 0};
int col[4] = {0, -1, 0, 1};

bool isValidandSafeObject(int x, int y, char mat[][N], int minDist[][N]){
    if ((x >= 0 && x < M) && (y >= 0 && y < N) ){
        if  (mat[x][y] == "O") && (minDist[x][y] == -1){
            return true;
        }
    }
    return false;
}

int[][N] updateDistance( char field[][N]){

    int minDist[M][N];
    queue<cell> Q;

    for (int i = 0; i < M ; i++){
        for ( int j = 0; j < N; j++){
            if (field[i][j] == "M"){
                Q.push({i, j})
                minDist[i][j] = 0;
            }
            else{
                minDist[i][j] = -1;
            }

        }
    }

    while (!Q.empty()){
        int x = Q.front().x;
        int y = Q.front().y;

        Q.pop();



    for( int k = 0; k < 4; k++){
            i = x + row[k];
            j = y + col[k];

            if isValidandSafeObject( i, j, field, minDist){
                Q.push({i, j});
                minDist[i][j] += 1;
            }
        }
    }

return minDist;
}

void main(){
    char mat[][N] =
    {
        {'O', 'M', 'O', 'O', 'X'},
        {'O', 'X', 'X', 'O', 'M'},
        {'O', 'O', 'O', 'O', 'O'},
        {'O', 'X', 'X', 'X', 'O'},
        {'O', 'O', 'M', 'O', 'O'},
        {'O', 'X', 'X', 'M', 'O'}
    };
    int minDist[M][N];


    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
            std::cout << std::setw(5) << updateDistance(mat);;
        std::cout << '\n';
    }


}

At line 20, if (mat[x][y] == "O") && (minDist[x][y] == -1){

I am getting the error ISO C++ forbids comparison between pointer and integer [-fpermissive]. I have searched stackoverflow, but none of the existing problems and solutions, seem to satisfy my problem.

I am using CodeBlocks. It would be very helpful, If someone could help me.

Sawradip Saha
  • 1,151
  • 11
  • 14
  • 4
    Unlike Python, there's a huge difference between `"0"`and `'0'` in C++. – molbdnilo Nov 02 '20 at 08:17
  • 3
    You should better use `std::vector` instead of plain C-arrays. If you have a C++ book that advises you to use plain C-arrays in situations as above then *get a new book*! – dtell Nov 02 '20 at 08:19
  • 2
    `updateDistance` returns a pointer to an object with automatic storage, which will lead to undefined behaviour if you try to access it. You probably want to check out the [book list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 02 '20 at 08:19
  • 1
    "ISO C++ forbids comparison" is the smallest of the problems of this code. There is a bunch of syntax errors. The main problem is that you cannot return a C-style array from a function. – n. m. could be an AI Nov 02 '20 at 08:57
  • @dtell agreed, I can't believe my school still teaches C++ with a book that promotes char arrays, `using namespace std`, turbo C++ and also has no idea what the STL library is –  Nov 02 '20 at 12:13

2 Answers2

1

'X' vs "X"

C++ doesn't treat these two the same way.
'X' is treated as a char in C++.

But the second you use "", the meaning changes. Now it is treated as a string literal. Or, a sequence of characters. Or, a char[].

char c = 'X'; 
char c = "X"; // how dare you

How pointers?

Back to our example, to make it clear, when I said "" implies a string literal or a sequence of characters, so when you do

char c = "x";

You are assigning a sequence of characters, to a char. It is equivalent to doing

char c = "Hello, World!"

So how does a pointer come in here? If you don't know anything at all about pointers, this will be a bit difficult to understand.

Pointers are just addresses. I could create a pointer and give it the adress of anything at all, an int, char, double.. doesn't matter, it's just gonna hold the memory address of that object.
In the same way, a char* would point to something that can be interpreted as a char.

int a = 5;
int ptr_to_a = &a; // ptr_t_a now holds the address of a

But there's something strange, take this code for example

const char* str = "foo"; // valid!

If pointers store addresses, how can it store a sequence of characters? C++ will secretly, or implicitly convert the following code into this.

char sequence[] = {'f','o','o'};

And after this, it will return the address of the first element, which is f, and assign it to the variable str. So now str points to the first element, in a character array. Hence you can assume that it will treat "foo" as a pointer. And since "" is implies a string literal, in your case it treats "O" as a pointer.

So we you do mat[x][y] == "O", you are comparing int == char*.
Therefore, mat[x][y] == "o"[0] will work because now you will access the first element of the array you just constructed.

Solution

The solution is to use '' instead.

if (mat[x][y] == 'O') //...

Also refer to this post, though it is the C language, the concept is the same.

-1

Characters in double quotes represent a pointer to a zero-end string of these characters ,no matter what the number of the characters is.

So "O" is not a char.It is a pointer to a zero-ended string.

'O' is a char.

Kbdman
  • 134
  • 1
  • 10
  • `"O"` is not a pointer. It's a string literal. Its type is array of chars, i.e. `const char[2]`. Arrays in some contexts decay to pointer, but an array is not a pointer. – bolov Nov 02 '20 at 12:11
  • @bolov It is again something really confusing in C++, especially to beginners who break their heads learning pointers and then see `const char* = "Hello world"` –  Nov 02 '20 at 12:14