Search code examples
clvaluervalue

what are l-value and r-value expression?


what is the order of precedence for pre ++, post ++ and * ? how these expression are parsed in VS 08 compiler.

 void main(){
        int arr[] ={34,11,43};
        int *ptr = arr;
        printf("%d",++*ptr++);
        printf("%d",++ptr++);
 }

explain the l value expression. i want to understand why ++*ptr++ is a valid expression, while ++ptr++ is giving error.

error: '++' needs l-value

Solution

  • ++p++
    

    Says: pre-increment p and post-increment p (in unspecified order). Even if it were allowed it would invoke undefined behavior due to modifying p more than once before encountering a sequence point.

    Anyway, the increment operators, post- and pre-, return an rvalue. An rvalue is the value of an expression. It has no location to write to and can be thought as an intermediate value.

    *p++
    

    This expression initially results in an lvalue, *p. That location can be written to, so it is incremented by the pre-increment and then p itself is incremented. The increment results in an rvalue, but you are not attempting to modify it.