Jo Viktor Jo Viktor - 25 days ago 9
C Question

Check if something at position X of array is a char

if (selecao > 0 && selecao < 5) {
if (assentosP[selecao] == 'O') {
printf("Test");


assentosP
is a
char
array, that has a bunch of either
D
or
O
letters (
D
by default)

char assentosP[2][4] = {
{'D', 'D', 'D', 'D'}, // [0][0], [0][1], [0][2]...
{'D', 'D', 'D', 'D'} // [1][0], [1][1], [1][2]...
};


What I need to know is a way to make
AssentosP
find out if the char in position
selecao
of the array is either
D
or
O
.

When I try it, it gives me a error


iso c++ forbids comparison between pointer and integer c

Answer

You defined a two dimensional array

char assentosP[2][4] = {
     {'D', 'D', 'D', 'D'}, // [0][0], [0][1], [0][2]...
     {'D', 'D', 'D', 'D'} // [1][0], [1][1], [1][2]...
     };

Thus expression assentosP[selecao] has type char[4]. You may not compare such an array with a character (or an integer after the integer promotion) like this

if (assentosP[selecao] == 'O') {

Take into account that if an array declared as having 4 elements then the valid range of indices is [0, 3]. Thus this statement

if (selecao > 0 && selecao < 5) {

looks incorrectly.

Comments