-
lvalue vs rvalue casting
Before I ask my question, here is a declaration:
unsigned char *c_a
Now I have this line:
(int)c_a += sizeof(whatever);
which used to work with an older compiler, but my new compiler complains with this error:
"error: invalid lvalue in assignment"
I've changed it to this line, which solves the compiler problem:
c_a += (int)sizeof(whatever);
Question is, are:
(int)c_a += sizeof(whatever);
and
c_a += (int)sizeof(whatever);
functionally equivalent? They seem to be, but I want to make sure I didn't do something stupid here.
Thanks.
-
Well if your intent is to advance the c_a pointer by whatever number of bytes there are in whatever, then there's no need for any casts at all.
-
Yeah, I agree.
You see, this isn't my code, I'm just making it work with a new compiler.
So I guess my question then becomes, is:
(int)c_a += sizeof(whatever);
equivalent to:
c_a += sizeof(whatever);
?
I've never seen casting done on the left hand side before, which is why I'm a bit confused on this matter.
Thanks.
-
Perhaps the old compiler is reading it as follows:
Code:
c_a = (int)c_a + sizeof(whatever);
-
Well lvalue casts have always been illegal, but that never stopped some compilers from accepting the syntax.
-
As to the original question, I wouldn't bet good money (at least: not my money) that the new line is equivalent to the old. You will need to read the documentation for the old compiler (or do tests with it) to work out what that line really does. Such constructs have always been illegal in C, so you are in the realm of a non-standard compiler extension.