Hi, I'm attempting the 8 queens problem (place 8 queens on a chess board with any of them attacking each other) in C99.
One of the things I was trying to do was to compare vars of int and size_t as shown here:
/* Attack all cells diagonal of pos (neg slope) */
for (int i = -8; i < boardObj->size; i++) {
printf("Coords: [%d, %d]\n", currentPos.x + i, currentPos.y + i);
if ((currentPos.x + i >= 0) &&
(currentPos.y + i >= 0)) {
boardObj->board[currentPos.x + i][currentPos.y + i] = ATTACKED;
}
}
/* Attack all cells diagonal of pos (pos slope) */
for (int j = -8; j < boardObj->size; j++) {
printf("Coords: [%d, %d]\n", currentPos.x - j, currentPos.y + j);
if ((currentPos.x - j >= 0) &&
(currentPos.y + j >= 0)) {
boardObj->board[currentPos.x - j][currentPos.y + j] = ATTACKED;
};
}
So my questions are:
- Why does the program skip the loops without casting the
boardObj->sizeof typesize_t? - Why does gcc only throw this up as a warning and not an error, allowing it to compile?
- Why does it work if
intis positive? Is it in terms of how the values are represented in binary? (1's or 2's complement or signed notation)