C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2008, 11:27 AM   #1
Registered User
 
Join Date: Jan 2005
Posts: 4
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.
fallout01 is offline   Reply With Quote
Old 07-10-2008, 11:41 AM   #2
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-10-2008, 03:27 PM   #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   Reply With Quote
Old 07-10-2008, 03:28 PM   #4
Deathray Engineer
 
MacGyver's Avatar
 
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   Reply With Quote
Old 07-12-2008, 12:16 AM   #5
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Old 07-12-2008, 02:35 AM   #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   Reply With Quote
Reply

Tags
casting, lvalue

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:01 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22