[comp.bugs.4bsd] lvalues and ++

mishkin@apollo.uucp (Nathaniel Mishkin) (12/12/86)

I am having problems with the construct:

    {
        long i;
        char *p; 

        *((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?

                -- Nat Mishkin
                   Apollo Computer Inc.
                   apollo!mishkin

chris@mimsy.UUCP (Chris Torek) (12/13/86)

In article <31da677c.809c@apollo.uucp> mishkin@apollo.uucp
(Nathaniel Mishkin) writes:
>I am having problems with the construct:
>	char *p; 
>	*((long *)p)++;

We just went through this one.  Is someone reduplicating old news
with new dates?

The construct is not legal C, though some compilers accept it.  A
cast is semantically equivalent to an assignment to an unnamed
temporary variable of the given type.  If the postincrement were
to do anything, it would have to increment that unnamed temporary,
so the change would be lost forever.  Any compiler that increments
`p' by `sizeof (long)' is doubly wrong: once for accepting the
illegal construct, and again for incrementing the wrong variable!
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris@mimsy.umd.edu

mouse@mcgill-vision.UUCP (der Mouse) (12/21/86)

In article <31da677c.809c@apollo.uucp>, mishkin@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@harvard.harvard.edu