cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
error: lvalue required as left operand o
error: lvalue required as left operand of assignment
Jun 6, 2014 at 6:31pm UTC
tdk93
(43)
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 UTC
Jun 6, 2014 at 6:34pm UTC
Irrelevant Elephant
(37)
Try using brackets:
int a = 10,b;
(a >= 5) ? b = 100 : b= 200;
printf("%d\n",b);
Jun 6, 2014 at 6:34pm UTC
tdk93
(43)
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 UTC
Jun 6, 2014 at 6:37pm UTC
Irrelevant Elephant
(37)
I think it may be because your compiler is misunderstanding the order of operations, so gets confused
Jun 6, 2014 at 6:59pm UTC
Auroch
(71)
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 UTC
cire
(8284)
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.