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

前端 未结 5 595
耶瑟儿~
耶瑟儿~ 2020-12-28 23:59
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*/
           


        
5条回答
  •  时光说笑
    2020-12-29 00:58

    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;
    

提交回复
热议问题