error: lvalue required as left operand of assignment

Jun 6, 2014 at 6:31pm
I don't understand the error which the following program gives on compilation.

1
2
3
int a = 10,b;
    a >= 5 ? b = 100 : b= 200;
    printf("%d\n",b);


The error is "lvalue required as left operand of assignment"
Last edited on Jun 6, 2014 at 6:31pm
Jun 6, 2014 at 6:34pm
Try using brackets:

int a = 10,b;
(a >= 5) ? b = 100 : b= 200;
printf("%d\n",b);
Jun 6, 2014 at 6:34pm
I understand that, but i want to know the reason why this error occurs, or what it means.
Last edited on Jun 6, 2014 at 6:35pm
Jun 6, 2014 at 6:37pm
I think it may be because your compiler is misunderstanding the order of operations, so gets confused
Jun 6, 2014 at 6:59pm
The left side of an assignment operator must be an addressable expression.
Addressable expressions include the following:
numeric or pointer variables
structure field references or indirection through a pointer
a subscripted array element

http://docwiki.embarcadero.com/RADStudio/XE6/en/E2277_Lvalue_required_%28C%2B%2B%29
Jun 6, 2014 at 7:17pm
In C, the ternary operator has a higher priority than = and your code is parsed thusly:

(a >= 5 ? b = 100 : b) = 200 ;

In C++ it will work as expected since both operators have the same priority.
Topic archived. No new replies allowed.