Thread: error: lvalue required as left operand of assignment

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    error: lvalue required as left operand of assignment

    Hi all,,

    I'm compiling a .c program using gcc compiler on linux,

    But , i received the error shown as "error: lvalue required as left operand of assignment"

    The error is due to the line of code as shown below

    (float *)pointers += stride;

    May I know how to debug this problem ?

    regards
    wj

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Generally this means you're trying to assign a value to a non-variable, or non-assignable variable (i.e. the name of an array can not be assigned a new value). For reasons I don't fully understand, the (float *) cast is causing this error. What exactly are you trying to do with that cast?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, a little research in the standard pulled up the following:
    Quote Originally Posted by C Standard, 6.3.2.1p1
    Anlvalue is an expression with an object type or an incomplete type other than void;(53)
    if an lvalue does not designate an object when it is evaluated, the behavior is undefined.
    When an object is said to have a particular type, the type is specified by the lvalue used to
    designate the object. A modifiable lvalue is an lvalue that does not have array type, does
    not have an incomplete type, does not have a const-qualified type, and if it is a structure
    or union, does not have any member (including, recursively, any member or element of
    all contained aggregates or unions) with a const-qualified type.

    (footnote 53) The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left
    operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an
    object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described
    as the ‘‘value of an expression’’.
    An obvious example of an lvalue is an identifier of an object. As a further example, if E is a unary
    expression that is a pointer to an object, *E is an lvalue that designates the object to which E points.
    Basically, this says you can only assign stuff to variables for which there is storage and for which it makes sense to change the value. That would exclude incomplete types, const variables, array names, and structures/unions with a const element somewhere in there.

    Quote Originally Posted by "C Standard
    Preceding an expression by a parenthesized type name converts the value of the
    expression to the named type. This construction is called a cast.(86)

    (footnote 86) A cast does not yield an lvalue. Thus, a cast to a qualified type has the same effect as a cast to the
    unqualified version of the type.
    So the cast creates a non-lvalue out of the left hand side of your +=, meaning you temporarily made pointers unassignable.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Dang Bayint! Why do you always have to show me up with your clever c-faq answers?

    Actually, the better question is: why in the world do I never remember to look for answers there myself?

    Thanks, though. One day I will remember that somebody has done much of the heavy lifting and I don't have to do it myself.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Perhaps he wanted to do
    Code:
    *(float *)pointers += stride;
    but it's hard to tell if he wanted to increase the pointer by the "size" bytes, or add to the value. The variable name 'stride' could imply either intention.
    Last edited by nonoob; 02-11-2011 at 12:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. Binary Tree Revisited: Near Completion
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2003, 08:23 AM
  3. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
  4. Release Configuration Problems
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2002, 04:54 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM