Difference between lvalue and rvalue in C++

In this article we will discuss the differences between lvalue and rvalue in C++.

In C, definition if lvalue and rvalue was somewhat simple, anything i.e. left of assignment operator is lvalue and  right ofassignment operator is rvalue. But in C++ this definition has changed and become more interesting. Every Expression in C++ is either rvalue or lvalue. Let’s discuss them one by one first.

What is lvalue ?

lvalue is anything whose address is accessible. It means we can take address of lvalue using & operator.  

Lets see some examples,

Example 1:

int x = 1;

In above expression, x is an lvalue because we can access the address of x i.e.

int * ptr = &x;

Example 2:

int z = x + 1;

In above expression, we can take address of z i.e.

int * ptr2 = &z;

Therefore, z is lvalue. But we cannot take the  address of  x+1 i.e.

Int * ptr3 = &(x+1); // Compile Error

Above line will not compile because we are trying to address of (x+1) and (x+1) doesn’t persist after single expression. As, we cannot take the address of (x+1). So, (x+1) is not an lvalue. So, what the heck is this (x+1) ?

As, lvalue is something whose address is accessible using & operator. But in this case, it’s not possible to access the address of (x+2). So, (x+2) is something that is not lvalue and that is exactly what rvalue is. (x+2) here is rvalue

What is rvalue ?

Rvalue is anything that is not lvalue. It means we cannot take address of rvalue and it also don’t persist beyond the single expression.  

Checkout some examples,

Example 3:

int x = 1;

Here you can not take the address of 1 i.e.

int * ptr = &(1); // Compile Error

Therefore, 1 is not a lvalue and hence it 1 is rvalue.

Example 4:

int a = x+1;

We cannot take the  address of  x+1 i.e.

int * ptr3 = &(x+1); // Compile Error

Hence (x+1) is not a lvalue and hence (x+1) is rvalue.

Lets look into some expression and find out what is lvalue and what is rvalue,

 

int a = 7; // a is lvalue & 7 is rvalue

int b = (a + 2); // b is lvalue & (a+2) is rvalue

int c = (a + b) ; // c is lvalue & (a+b) is rvalue

int * ptr = &a; // Possible to take address of lvalue

//int * ptr3 = &(a + 1);  // Compile Error. Can not take address of rvalue

Now, lets create a function that returns a integer i.e

int getData()
{
	int data = 0;
	return data;
}

Here, the value retutned by getData() is temporary i.e. it will not persist beyond the expression where it is called. Therefore it will get copied. Therefore, we can not take the address of temporary value returned by getData() i.e.

int * ptr = &getData(); // Compile error - Cannot take address of rvalue

Therefore, getData() is a rvalue and we cannot take the address of rvalue.

[showads ad=inside_post]

 

Thanks.

 

3 thoughts on “Difference between lvalue and rvalue in C++”

  1. Purna chandra nanda

    Hi,

    Please consider the below scenario.

    int x =10;
    int y =x; // Here we can get the address of x.

    Could you please confirm whether x is rvalue or lvalue ?

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top