variable type cast

Prof A Olowofoyeku (The African Chief) chief at greatchief.plus.com
Mon May 13 17:23:45 CEST 2013


On 12 May 2013 at 16:12, Jay Michael wrote:

> gpc version 20070904, based on gcc-3.4.5 (mingw special)
>  
>      Given
>           type INT16  = INTEGER  attribute( size=16 ) ;
>           type WORD16 = Cardinal attribute( size=16 ) ;
>           var i16 : INT16 ;
>           var w16 : WORD16 ;
> why does
>           WORD16(I16) := W16 ;
> produce "error: invalid lvalue in assignment"?
           
int16 and word16 have different ranges, and that kind of type-casting 
will not circumvent the fact that the compiler should stop you from 
doing things like that unless you specifically instruct it to NOT do 
so.  

"i16 := Word16(w16)" should compile, as should "i16 := w16". 
Preferably, all should be avoided, because, if the value of "w16" is 
higher than the int16 range, the program may simply die with a runtime 
error (at best), or, worse, you end up with an undefined value in 
"i16", which may then lead to unpredictable behaviour in your program.  

Imagine this;
w16 := 45000;
i16 := w16;
This should compile, but the assignment will generate a run-time error. 

If you turn off range checking;
w16 := 45000;
{$R-}
i16 := w16;
{$R+}
then you will not get a runtime error, but what would the value of 
"i16" be after the assignment? Would it be what you are expecting?

Best regards, The Chief
--------
Prof. Abimbola A. Olowofoyeku (The African Chief) 
web:  http://www.greatchief.plus.com/




More information about the Gpc mailing list