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"?
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/
On Sun, May 12, 2013 at 04:12:45PM -0700, 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"?
I would think that the reason is that to the left of the assignment operator you need a variable or equivalent, in C jargon also known as an lvalue (i.e. something that has a persistent memory address).
Note that a variable by itself is both an lvalue and an expression (an rvalue, though this term is little used), but not every expression is an lvalue (e.g. v + 1 is not an lvalue; you cannot assign anything to it).
In particular, casting is an operator that yields an rvalue, but not an lvalue. Casting needs to done to the right of the assignment operator.
(The previous warnings about casting still stand.)
Tom
Tom Verhoeff wrote:
In particular, casting is an operator that yields an rvalue, but not an lvalue. Casting needs to done to the right of the assignment operator.
Not really. A casting at the right of the assignment operator is called a value type cast, a casting at the left of the assignment operator is called a variable type cast. The rules for both are a bit different, e.g. a value type cast may allow a cast to a different size (e.g. to a different size integer), a variable type cast is more strict.
Regards,
Adriaan van Os
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"?
A bug in the compiler. GPC is too picky when it comes to typecasts.
Regards,
Adriaan van Os