I'm trying to parse a Fen String (char *starting_fen)
, but it doesn't seem to be working.
starting_fen
: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
void init_board(char *starting_fen)
{
int rank = 7, file = 0, fen_length = strlen(starting_fen);
for (int i = 0; i < fen_length; i )
{
if (starting_fen[i], "/") // <-- works for all characters?
{
printf("SYMBOL @ idx %d ==> '%c'\n", i, starting_fen[i]);
// file = 0;
// rank--;
}
}
}
This is my output:
SYMBOL @ idx 0 ==> 'r'
SYMBOL @ idx 1 ==> 'n'
SYMBOL @ idx 2 ==> 'b'
//...
SYMBOL @ idx 53 ==> '0'
SYMBOL @ idx 54 ==> ' '
SYMBOL @ idx 55 ==> '1'
I tried using strcmp
(if (strcmp(starting_fen[i], "/"))
) but got a Segmentation fault...
CodePudding user response:
I answer this because it is a funny bug. Here:
if (starting_fen[i], "/")
You are calling ,
operator. So it returns "/"
which always will be evaluated to true
(Since it is a address which will have a value not equal to 0
). Your code need to be like this:
if (starting_fen[i] == '/')
So 2 changes:
- You need
==
operator to check equality. - Characters are identified by
'
not"
.
CodePudding user response:
This fails:
if (starting_fen[i], "/") {
because starting_fen[i], "/"
is always "/" which is not null. See the wikipdia page about the comma operator
This fails:
if (strcmp(starting_fen[i], "/")) {
Because starting_fen[i]
will be a char, not a pointer to a string. Thus it will try to read a location in early memory and fail.
This fails:
if (starting_fen[i] == "/") {
Since you are trying to compare a char to a pointer.
This is correct:
if (starting_fen[i] == '/') {