Why using ++i++ gives an error?

raj_ksrt's Avatar, Join Date: Feb 2009
Newbie Member
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
raj_ksrt's Avatar, Join Date: Feb 2009
Newbie Member
But why the following code works?



Code:
int main()
{
int b=10;
int *j=&++b;
++*j++;
}
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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++)).