Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Why using ++i++ gives an error? (http://www.go4expert.com/forums/using-i-error-t16403/)

raj_ksrt 3Mar2009 13:13

Why using ++i++ gives an error?
 
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.

xpi0t0s 3Mar2009 13:32

Re: Why using ++i++ gives an error?
 
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.

raj_ksrt 3Mar2009 14:58

Re: Why using ++i++ gives an error?
 
But why the following code works?



Code:

int main()
{
int b=10;
int *j=&++b;
++*j++;
}


xpi0t0s 3Mar2009 16:01

Re: Why using ++i++ gives an error?
 
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++)).


All times are GMT +5.5. The time now is 23:30.