Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
}
like image 560
Kishan Kumar Avatar asked Oct 27 '15 17:10

Kishan Kumar


People also ask

What does lvalue required as left operand of assignment mean in C?

lvalue required as left operand of assignment. lvalue means an assignable value (variable), and in assignment the left value to the = has to be lvalue (pretty clear). Both function results and constants are not assignable ( rvalue s), so they are rvalue s.

What is the error lvalue required in C?

This error occurs when we put constants on left hand side of = operator and variables on right hand side of it. Example: #include <stdio.h> void main()

What is lvalue in C programming?

CServer Side ProgrammingProgramming. An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address).

What is lvalue in Arduino?

Which Arduino has a pin 60? In the C programming language, a lvalue is something you can assign a value to and an rvalue is a value that can be assigned. The names just mean 'left side of the equals' and 'right side of the equals'.


1 Answers

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;
like image 124
R Sahu Avatar answered Sep 24 '22 17:09

R Sahu