When do we get \"l-value required\" error...while compiling C++ program???(i am using VC++ )
Try to compile:
5 = 3;
and you get error: lvalue required as left operand of assignment
This happens when you're trying to assign to something (such as the result of a scalar function) that you can't assign to.
We assign value to a variable. If we try to do the reverse thing then L-value errors occur.
int x,y,z;
x=1;
y=2;
z=x+y; //Correct
x+y=z; //L-value required
An "lvalue" is a value that can be the target of an assignment. The "l" stands for "left", as in the left hand side of the equals sign. An rvalue is the right hand value and produces a value, and cannot be assigned to directly. If you are getting "lvalue required" you have an expression that produces an rvalue when an lvalue is required.
For example, a constant is an rvalue but not an lvalue. So:
1 = 2; // Not well formed, assigning to an rvalue
int i; (i + 1) = 2; // Not well formed, assigning to an rvalue.
doesn't work, but:
int i;
i = 2;
Does. Note that you can return an lvalue from a function; for example, you can return a reference to an object that provides a operator=().
As pointed out by Pavel Minaev in comments, this is not a formal definition of lvalues and rvalues in the language, but attempts to give a description to someone confused about an error about using an rvalue where an lvalue is required. C++ is a language with many details; if you want to get formal you should consult a formal reference.
R Value is an expression that always appear on right side of an assignment operator Eg:
int a = 5;//here 5 is Rvalue
L Value is an expression that can either come on left or right side of an assignment.When it is in on left side it refers to a place that can hold a value.
Here
a
in expressiona = 5
is L Value
and when appearing on right side value is read from the L Value. Since R value which does not have capability to locate any memory it cannot hold any value like LValue so
5 = 6 or 5 = a
will be compiler error.