This should have given an error, but why..?

Posted By: Mr Wurm

This should have given an error, but why..? - 01/10/09 16:40

So I was happily coding away and all of a sudden I got this strange bug in my code. Took quite some time to find it. Primarily because it wasn't where I expected it to be.

Code:
var interface_cardnum_to_xy(char par_card_num,char x_or_y){
	var par_x;
	var par_y;
	
	switch(par_card_num){
		case 0:	par_x = 0; par_y = 0;	break;
		
		case 1:  par_x = 10; par_y = 10;   break;
		case 2:  par_x = 20; par_y = 10;   break;
                //lotsa more cases here
        }
	if(x_or_y = 0) return(par_x);
	if(x_or_y = 1) return(par_y);
}



what the code does is basically return me the x or y coordinates (depending on the second parameter) for the card id number (the first card id number).

Now heres the deal. The error is quite obviously to be found in the last two lines where it should be if(x_or_y == 0) rather than just '='. But why isn't this output as a syntax error? Am I overlooking something fundamental here?
Posted By: Mr Wurm

Re: This should have given an error, but why..? - 01/13/09 00:18

...a small, barely audible and yet slightly shifty *bump* comes along and steals a minuscule split second of your time...
Posted By: Joozey

Re: This should have given an error, but why..? - 01/13/09 01:15

It's not a syntax error because it's valid syntax.

if(x == 5) is the comparison x with 5.
if(x = 5) is a comparison with x set to 5, thus true.
if(x = 0) is a comparison with x, but set to 0, thus not true.
© 2024 lite-C Forums