I'm trying to compare two unsigned char variables in an if statement.
For some reason, even when I hard code the two values to be different, the if statement keeps returning true.
Clearly I'm doing something wrong here - would anyone be able to point out what?
Originally I was trying to pull a value from an array and compare it:
So I would declare and set my array and my check digit:
And then later, in a function, compare the two:
These seems to return true for all four values in the array, even though all of them are zero! It does the same if i set it to ==.
I then tested with just the if statement:
Both of these run update_Enemy_Sprite, even though both should be false.
I can find nothing on the internet that says I am doing this incorrectly. Where am I going wrong?
Thanks,
For some reason, even when I hard code the two values to be different, the if statement keeps returning true.
Clearly I'm doing something wrong here - would anyone be able to point out what?
Originally I was trying to pull a value from an array and compare it:
So I would declare and set my array and my check digit:
Code:
unsigned char enemy_onscreen_value = 1;
unsigned char enemy_onscreen_check[] = {
0,0,0,0
};
unsigned char enemy_onscreen_check[] = {
0,0,0,0
};
And then later, in a function, compare the two:
Code:
void update_Enemies(void) {
for(index2 = 0; index2 < sizeof(enemy_onscreen_check); ++index2){
if(enemy_onscreen_check[index2] >= enemy_onscreen_value) {
update_Enemy_Sprite(index2);
}
}
}
for(index2 = 0; index2 < sizeof(enemy_onscreen_check); ++index2){
if(enemy_onscreen_check[index2] >= enemy_onscreen_value) {
update_Enemy_Sprite(index2);
}
}
}
These seems to return true for all four values in the array, even though all of them are zero! It does the same if i set it to ==.
I then tested with just the if statement:
Code:
unsigned char enemy_onscreen_value = 1;
unsigned char enemy_onscreen_1 = 0;
void update_Enemies(void) {
if(enemy_onscreen_value == enemy_onscreen_1) {
update_Enemy_Sprite(index2);
}
}
unsigned char enemy_onscreen_1 = 0;
void update_Enemies(void) {
if(enemy_onscreen_value == enemy_onscreen_1) {
update_Enemy_Sprite(index2);
}
}
Both of these run update_Enemy_Sprite, even though both should be false.
I can find nothing on the internet that says I am doing this incorrectly. Where am I going wrong?
Thanks,