lvalue vs rvalue casting

This is a discussion on lvalue vs rvalue casting within the C Programming forums, part of the General Programming Boards category; Before I ask my question, here is a declaration: unsigned char *c_a Now I have this line: (int)c_a += sizeof(whatever); ...

  1. #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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #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.

  4. #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);

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,660
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lvalue rvalue discussion
    By George2 in forum C++ Programming
    Replies: 18
    Last Post: 03-04-2008, 05:48 AM
  2. Get lvalue through rvalue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2007, 08:53 AM
  3. Function argument not converted to reference
    By MWAAAHAAA in forum C++ Programming
    Replies: 14
    Last Post: 10-22-2006, 04:24 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 10:23 PM

Tags for this Thread


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