Pointers in an external function not working as they should?

Hi.

Im trying work with pointers and failing a little, although I've made strides recently.

Is there a reason that a code similar to the first example would not behave like the second?

example 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int coldet(wall* W, entity* E){
   if ( W->x < E->x && E->x < W->x + W->w ){
   return 1;
   }
   else return 0;   
}

int main(){
   switch( coldet( wall1, entity1) ){ //more  cases in actual code
   case 1:
   entity1->editpos(wall1->x);
   case 0:;
   }   
}


example 2

1
2
3
4
5
main(){
    if ( wall1->x < entity1->x && entity1->x < wall1->x + wall1->w ){
       entity1->editpos(wall1->x);
    }
}



Last edited on
Well, yes, the first won't compile..

*wall W should be wall* W, same for entity.
Also, what's with the case outside a switch?

Another problem both codes have is the expression a < b < c. It wont work. Operators are evaluated one at a time, so this is equal to (a < b) < c. a < b can be only true or false, so you're comparing 1 or 0 to c.
This should be a < b && b < c instead.

Also, main() should be an int function and return 0.

Also, in the first example, if would be much more appropriate than a switch, since there can be only two values.
Last edited on
Firstly sorry for the bad code i will edit it in a second - i was playing fifa at the same time.

This is a simplified version of my code which compiles fine.

Thanks for the reply but the real question i was asking is the part about their behaviour apparently being different and i dont understand why.

Last edited on
Well, your case doesn't have a break.. (so all cases will be evaluated)
Other than that, no. Putting things in a function should not make any difference here.
I have actually missed off break on each case, that could solve it.
You are a legend. I've been struggling with that problem for so long and it was that simple. Didn't even consider my switch statement could be wrong.

THANKYOU
Topic archived. No new replies allowed.