16
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();
}
3
  • 3
    What do you expect the result of p + 1 = p to be?
    – John Bode
    Oct 27, 2015 at 18:19
  • 2
    This is no C code, but C++. Change the tag and title! Oct 27, 2015 at 18:47
  • 1
    p + 1 and p are both single addresses in the memory, you can't assign one address to another one
    – Student
    Sep 10, 2021 at 8:27

5 Answers 5

40

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;
0
5

Put simply, an lvalue is something that can appear on the left-hand side of an assignment, typically a variable or array element.

So if you define int *p, then p is an lvalue. p+1, which is a valid expression, is not an lvalue.

If you're trying to add 1 to p, the correct syntax is:

p = p + 1;
4

To assign, you should use p=p+1; instead of p+1=p;

int main()
{

   int x[3]={4,5,6};
   int *p=x;
   p=p+1; /*You just needed to switch the terms around*/
   cout<<p<<endl;
   getch();
}
0
3

if you use an assignment operator but use it in wrong way or in wrong place, then you'll get this types of errors!

suppose if you type:
p+1=p; you will get the error!!

you will get the same error for this:
if(ch>='a' && ch='z')
as you see can see that I i tried to assign in if() statement!!!
how silly I am!!! right??
ha ha
actually i forgot to give less then(<) sign
if(ch>='a' && ch<='z')
and got the error!!

0

It is just a typo(I guess)-

p+=1;

instead of p +1=p; is required .

As name suggest lvalue expression should be left-hand operand of the assignment operator.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.