lvalue required as left operand of assignment error when using C++

Kishan Kumar picture Kishan Kumar · Oct 27, 2015 · Viewed 173.6k times · Source
int main()
{

   int x[3]={4,5,6};
   int *p=x;
   p +1=p;/*compiler shows error saying 
            lvalue required as left 
             operand of assignment*/
   cout<<p 1;
   getch();
}

Answer

R Sahu picture R Sahu · Oct 27, 2015

When you have an assignment operator in a statement, the LHS of the operator must be something the language calls an lvalue. If the LHS of the operator does not evaluate to an lvalue, the value from the RHS cannot be assigned to the LHS.

You cannot use:

10 = 20;

since 10 does not evaluate to an lvalue.

You can use:

int i;
i = 20;

since i does evaluate to an lvalue.

You cannot use:

int i;
i + 1 = 20;

since i + 1 does not evaluate to an lvalue.

In your case, p + 1 does not evaluate to an lavalue. Hence, you cannot use

p + 1 = p;