Why using ++i++ gives an error?

Newbie Member
3Mar2009,13:13   #1
raj_ksrt's Avatar
Hi all,

Why can't use post increment operator along with & or ++ operators ? The compiler complains that it needs an lvalue when i say ++i++ or &i++.


Please let me know what happens internally with the above two expressions.

thanks.
Mentor
3Mar2009,13:32   #2
xpi0t0s's Avatar
Because i++ evaluates to a number, not a variable. You can't do ++i++ or &i++ for the same reason you can't do ++5 or &5; neither make sense.
Newbie Member
3Mar2009,14:58   #3
raj_ksrt's Avatar
But why the following code works?



Code:
int main()
{
int b=10;
int *j=&++b;
++*j++;
}
Mentor
3Mar2009,16:01   #4
xpi0t0s's Avatar
int *j=&++b;
++b in value context evaluates to b, which is an lvalue, so its address can be taken.

++*j++;
Preincrement *j and postincrement j. No problem there. Except j will no longer be pointing at a valid object (it points to (&b)+4). You can always preincrement a dereferenced pointer.

I guess you're misreading ++*j++ as ++(*j)++, since you seem to be comparing it with ++i++. ++*j++ is valid, but ++(*j)++ is not (you get the same error as ++i++), and is a different expression due to operator precedence. ++*j++ is equivalent to ++(*(j++)).