What is the Lvalue and Rvalue?

Answer Posted / ashish

An object is a contiguous region of memory storage. An
lvalue (pronounced: L value) is an expression that refers to
such an object. The original definition of lvalue referred
to "an object that can appear on the left-hand side of an
assignment." However, const objects are lvalues, and yet
they cannot appear on the left-hand side of an assignment.
An expression that can appear in the right-hand side of an
expression (but not in the left-hand side of an expression)
is an rvalue. For example:


#include <string>
using namespace std;
int& f();

void func()
{
int n;
char buf[3];
n = 5; // n is an lvalue; 5 is an rvalue
buf[0] = 'a'; //buf[0] is an lvalue, 'a' is an rvalue
string s1 = "a", s2 = "b", s3 = "c"; // "a", "b", "c" are
rvalues
s1 = // lvalue
s2 +s3; //s2 and s3 are lvalues that are implicitly
converted to rvalues
s1 =
string("z"); // temporaries are rvalues
int * p = new int; // p is an lvalue; 'new int' is an
rvalue
f() = 0; // a function call that returns a reference is
an lvalue
s1.size(); // otherwise, a function call is an rvalue
expression
}


An lvalue can appear in a context that requires an rvalue;
in this case, the lvalue is implicitly converted to an
rvalue. An rvalue cannot be converted to an lvalue.
Therefore, it is possible to use every lvalue expression in
the example as an rvalue, but not vice versa.

Is This Answer Correct ?    3 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Do you know what are bitwise shift operators in c programming?

801


What is the difference between pure virtual function and virtual function?

850


Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?

863


What is the difference between specifying a constant variable like with constant keyword and #define it? i.e what is the difference between CONSTANT FLOAT A=1.25 and #define A 1.25

1695


What is the difference between int main and void main?

773


What does 2n 4c mean?

966


What is a buffer in c?

750


How can you tell whether a program was compiled using c versus c++?

824


Why header file is used in c?

761


how to create duplicate link list using C???

2258


Can the size of an array be declared at runtime?

792


What are the 5 types of organizational structures?

761


What does sizeof int return?

812


What are the features of c language?

812


Ow can I insert or delete a line (or record) in the middle of a file?

766