lvalues and ++
der Mouse
mouse at mcgill-vision.UUCP
Sun Dec 21 16:19:51 AEST 1986
In article <31da677c.809c at apollo.uucp>, mishkin at apollo.uucp (Nathaniel Mishkin) writes:
> I am having problems with the construct:
> *((long *)p)++;
> Some C compilers (e.g. the one sent with 4.3bsd) complain with the error:
> illegal lhs of assignment operator
> Other compilers seem to handle this correctly (incrementing by 4, by
> the way).
> Is this invalid C or is the 4.3bsd compiler broken?
It is invalid C. A cast does not produce an lvalue. If you want this
effect then try
p = (whatever *) (1 + (long *)p)
(if you want to use the value as well, try
((long *)(p=(whatever *)(1+(long *)p)))[-1]
-- yech.)
As someone on comp.lang.c pointed out when this question came up a
while ago (it is one of the periodic questions), any compiler that
"handle[s] this correctly" is broken twice - once for accepting it and
again for modifying p (since "(long *)p" is a temporary, so the ++
should change the temporary, which will then get thrown away).
der Mouse
USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse
think!mosart!mcgill-vision!mouse
Europe: mcvax!decvax!utcsri!mcgill-vision!mouse
ARPAnet: think!mosart!mcgill-vision!mouse at harvard.harvard.edu
More information about the Comp.lang.c
mailing list