Ho il seguente codice di'confronto è sempre vero a causa di un intervallo limitato di tipo di dati' avviso in C?
//Point.h
#define WIDTH 8
#define HEIGHT 8
typedef struct Point
{
char x;
char y;
} Point;
//Board.c
#include <stdbool.h>
// Some other functions that we don't care about...
bool inBounds(Point * p)
{
return p->x >= 0
&& p->x <= WIDTH
&& p->y >= 0
&& p->y <= HEIGHT;
}
Quando compilo questo (PPU-gcc 4.1.1), ottengo il seguente avvertimento
warning: comparison is always true due to limited range of data type
anche se la gamma di char è -127 a 127 e WIDTH è 8, che è ben all'interno del range di un char. Ho già provato un cast esplicito di WIDTH in un carattere, ma ho ancora ottenuto l'errore.
basta cambiare la tua struct a int, e vedere cosa happend? –
Nel tuo caso, devi eseguire il totoi o il cast statico per xey, quindi confrontare con altezza e larghezza. –