invalid lvalue in assignment

Discussion in 'C Programming' started by mdh, Oct 28, 2007.

  1. mdh

    mdh Guest

    May I ask why this works:

    given:
    char s[];
    char *posbfr = s;
    char *endbfr = s + MAXOP;

    void(...){
    if (posbfr >= endbfr)
    printf("......");
    else
    *posbfr++ = c ;
    }


    but this is invalid?

    (posbfr >= endbfr) ? printf("......") : *posbfr++ = c ;

    (with an "invalid lvalue in assignment") error?

    or...is this the dumbest question yet? :)

    Thank you.
    mdh, Oct 28, 2007
    #1
    1. Advertising

  2. mdh

    Ian Collins Guest

    mdh wrote:
    > May I ask why this works:
    >
    > given:
    > char s[];
    > char *posbfr = s;
    > char *endbfr = s + MAXOP;
    >
    > void(...){
    > if (posbfr >= endbfr)
    > printf("......");
    > else
    > *posbfr++ = c ;
    > }
    >
    >
    > but this is invalid?
    >
    > (posbfr >= endbfr) ? printf("......") : *posbfr++ = c ;
    >

    You are assigning c to the result of the conditional expression, so the
    above equivalent to

    ((posbfr >= endbfr) ? printf("......") : *posbfr++) = c ;

    --
    Ian Collins.
    Ian Collins, Oct 28, 2007
    #2
    1. Advertising

  3. mdh wrote:

    > May I ask why this works:

    <snip>
    > but this is invalid?
    >
    > (posbfr >= endbfr) ? printf("......") : *posbfr++ = c ;
    >
    > (with an "invalid lvalue in assignment") error?


    Assignment is lower priority than ?:. The compiler sees your expression
    as (cond ? x : y) = c;

    Try explicit parentheses, like this:

    (posbfr >= endbfr) ? printf("......") : (*posbfr++ = c) ;
    Peter Pichler, Oct 28, 2007
    #3
  4. mdh

    mdh Guest

    On Oct 28, 4:02 pm, Ian Collins <> wrote:
    > mdh wrote:
    >
    > > but this is invalid?

    >
    > > (posbfr >= endbfr) ? printf("......") : *posbfr++ = c ;

    >


    > Ian Collins wrote:


    > You are assigning c to the result of the conditional expression, so the
    > above equivalent to
    >
    > ((posbfr >= endbfr) ? printf("......") : *posbfr++) = c ;
    >




    Ian,
    I know I am not then understanding this. I thought, given

    expr1 ? expr2 : expr3;

    then, if expr1 evaluates to true, then expr2 is evaluated, else if
    false, expr3.

    So, in the above case, is an assignment not regarded as a valid
    expression?
    ..
    mdh, Oct 28, 2007
    #4
  5. mdh

    mdh Guest

    On Oct 28, 4:03 pm, Peter Pichler <> wrote:
    > mdh wrote:
    > > May I ask why this works:

    >
    >
    > Try explicit parentheses, like this:
    >
    > (posbfr >= endbfr) ? printf("......") : (*posbfr++ = c) ;


    Aha...thank you.
    mdh, Oct 28, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A. Farber
    Replies:
    5
    Views:
    612
    A. Farber
    Oct 6, 2006
  2. Kavya
    Replies:
    9
    Views:
    493
    Dik T. Winter
    Oct 28, 2006
  3. Replies:
    11
    Views:
    683
    James Kuyper
    Sep 22, 2008
  4. Chad

    invalid lvalue in assignment

    Chad, Jan 9, 2009, in forum: C Programming
    Replies:
    16
    Views:
    981
    Default User
    Jan 10, 2009
  5. Replies:
    2
    Views:
    1,865
Loading...

Share This Page