c++ - L-Value, Pointer arithmetic -


this question has answer here:

i looking explanation of how lines l1 , l2 in code snippet below differ w.r.t l-values, i.e, why getting the: c2105 error in l1, not in l2?

*s = 'a'; printf("%c\n", *s ); //printf("%c\n", ++(*s)++ ); //l1 //error c2105: '++' needs l-value printf("%c\n", (++(*s))++);  //l2 printf("%c\n", (*s) ); 

note: got above result when code compiled .cpp file. now, on compilation .c file, same error c2105 on both lines l1 , l2. why l2 compile in c++, , not in c mystery :(.

if of help, i'm using visual c++ express edition.

compiler see ++(*s)++ ++((*s)++), post-increment has higher precedence pre-increment. after post-incrementation, (*s)++ becomes r-value , can't further pre-incremented (here).
, yes not case of ub (at least in c).
, read answer.

for l2 in c++ not giving error because
c++11: 5.3.2 increment , decrement says:

the operand of prefix ++ modified adding 1, or set true if bool (this use deprecated). operand shall modifiable lvalue. type of operand shall arithmetic type or pointer completely-defined object type. the result updated operand; lvalue, , bit-field if operand bit-field. if x not of type bool, expression ++x equivalent x+=1.

c++11:5.2.6 increment , decrement says:

the value of postfix ++ expression value of operand. [ note: value obtained copy of original value —end note ] operand shall modifiable lvalue. type of operand shall arithmetic type or pointer complete object type. value of operand object modified adding 1 it, unless object of type bool, in case set true. value computation of ++ expression sequenced before modification of operand object. respect indeterminately-sequenced function call, operation of postfix ++ single evaluation. [ note: therefore, function call shall not intervene between lvalue-to-rvalue conversion , side effect associated single postfix ++ operator. —end note ] the result prvalue. type of result cv-unqualified version of type of operand.

and on msdn site stated that:

the operands postfix increment , postfix decrement operators must modifiable (not const) l-values of arithmetic or pointer type. type of result same of postfix-expression, it no longer l-value.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -