![]() |
| | #1 |
| Registered User Join Date: Jan 2005
Posts: 4
| lvalue vs rvalue casting 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. |
| fallout01 is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jan 2005
Posts: 4
| 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. |
| fallout01 is offline | |
| | #4 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| Perhaps the old compiler is reading it as follows: Code: c_a = (int)c_a + sizeof(whatever);
__________________ |
| MacGyver is offline | |
| | #5 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Well lvalue casts have always been illegal, but that never stopped some compilers from accepting the syntax.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #6 |
| Registered User Join Date: Jun 2005
Posts: 1,343
| 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. |
| grumpy is offline | |
![]() |
| Tags |
| casting, lvalue |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| lvalue rvalue discussion | George2 | C++ Programming | 18 | 03-04-2008 05:48 AM |
| Get lvalue through rvalue | George2 | C++ Programming | 4 | 12-17-2007 08:53 AM |
| Function argument not converted to reference | MWAAAHAAA | C++ Programming | 14 | 10-22-2006 03:24 AM |
| Question on l-values. | Hulag | C++ Programming | 6 | 10-13-2005 04:33 PM |
| Why wont my function exit correctly? | LightsOut06 | C Programming | 2 | 10-09-2005 09:23 PM |