Explanation, please!

Robert Alverson alverson at decwrl.dec.com
Sat Sep 3 03:27:48 AEST 1988


In article <9064 at pur-ee.UUCP> hankd at pur-ee.UUCP (Hank Dietz) writes:
>	Incidentally, this ran about 8x faster (on a VAX 11/780) than using
>	the usual copy loop.  Unfortunately, the above code should have been
>	written as:
>
>	if (n & 512) {
>		*(((struct t512 *) q)++) = *(((struct t512 *) p)++);
>	}
>	...
>
>	but, for some unknown reason, the VAX C compiler didn't like that.
>
>
>Enjoy.
>					hankd at ee.ecn.purdue.edu
Actually, the VAX C compiler was completely correct.  Attempting
to post-increment the result of a cast is not legal C.  One way
around this is to use a little extra indirection:

    char* p;

    /* was:   ((int *)p)++; */
    (*((int **) &p))++;		/* phew! */

The reasoning is that the result of a cast is an expression, not a
lvalue.  Quite often, compilers relax this rule for coercions which
produce no code.  In this case, it looks like the VAX C doesn't

Bob



More information about the Comp.lang.c mailing list