user1223540
user1223540

Reputation: 83

how to check strings array's 1st elements 1st symbol with if?

So basically I got string array, lets say a[i][b];

so the code looks something like this -

for(int i = 0; i < 3; i++) {
  for(int n = 0; b < 3; b++) {
    if(a[i][b] == "s") {
     cout << a[i][b] << endl;
    }
  }
}

the array exists, and I can check it if I just show on console the a[i][b] without if statement, but with if statement it gives me this error -

error: ISO C++ forbids comparison between pointer and integer

Is there any way to fix that?

Upvotes: 1

Views: 138

Answers (3)

shift66
shift66

Reputation: 11958

put s in single quotes like this 's'
"s" is a text and in C++ there is no String class (native). So "s" is actually a pointer to character sequence and a[i][b] is just a single character.

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96276

"s" is a C string literal, if you want to compare with a character use 's'.

Upvotes: 3

BoBTFish
BoBTFish

Reputation: 19767

"s" is a string literal, i.e. a character array, so decays to a pointer. To just compare to a character, use single quotes:

if (a[i][b]=='s')

Upvotes: 5

Related Questions