1

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
Rohit Bohara
  • 323
  • 4
  • 14

3 Answers3

3

The precedence is ++(*(ptr++)) and ++(ptr++) respectively.

ptr++ is an rvalue, because the language definition says so. (rvalue means that you can use the value but you cannot attempt to refer to the memory location where this value might be stored).

The ++ operator updates the value stored in a memory location, therefore it can only be applied to an lvalue. So ++(ptr++) is a constraint violation.

However, * can be applied to rvalues and the result is an lvalue (i.e. an expression that designates a memory location). So ++ can be applied to *ptr++ .

M.M
  • 138,810
  • 21
  • 208
  • 365
  • ptr+1 is also an rvalue "because the language definition says so" but there's more to it than that ... an expression that isn't a dereference doesn't make sense as a target for a value. – Jim Balter Aug 10 '14 at 07:16
  • what does this statement print?--> printf("%d",++*ptr++); 35 or 12 ? – piyukr Aug 10 '14 at 07:45
  • 1
    @piyukr work out what you think it should print, then try it and see – M.M Aug 10 '14 at 07:51
2
++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.

Community
  • 1
  • 1
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • i want to understand why ++*p++ is valid and ++p++ is not valid – Rohit Bohara Aug 10 '14 at 05:54
  • "++p++ Says: pre-increment p and post-increment p (in unspecified order)" -- this is wrong. See Matt McNabb's answer. "This expression initially results in an rvalue, *p" -- `*p` is an lvalue. "then p itself is incremented. The increment returns an rvalue" -- this is nonsensical; the increment is a side-effect, it doesn't "return" anything, and might not occur until the sequence point. – Jim Balter Aug 10 '14 at 07:20
  • Further: "This expression initially results in an rvalue, *p" -- no, it results in the content of `*p`, which is passed to `printf`. Expressions and the values they yield are different things. – Jim Balter Aug 10 '14 at 07:29
0

In C programming preincrement and predecrement operators return an rvalue which essentially tells you that it is a result and not a place to make an assignment. In C++ programming the preincrement and predecrement operators return an lvalue where you can perform further assignments.

A statement like,

int i=10;
++i=5;

is syntactiacally valid in C++. But in C it is a syntax error.

Even in C++ the postincrement and postdecrement operators return an rvalue. Thus the following statement will give you a syntax error in both C and C++.

int i=10;
i++=5;
Deepu
  • 7,592
  • 4
  • 25
  • 47
  • "++i=5; is syntactiacally valid in C++" -- but is pointless since the `++` is superseded by the `=`, resulting in `i` being 5. – Jim Balter Aug 10 '14 at 07:37