When is a cast not a cast?

Chris Torek chris at mimsy.UUCP
Thu May 25 13:36:23 AEST 1989


In article <6200006 at ux1.cso.uiuc.edu> phil at ux1.cso.uiuc.edu writes:
>    long x,y,z,*q,*p,a[19];
>    int i;
>      .
>      .
>    p = a;
>    q = NULL[i];  /* or even i[NULL] */
>    x = *(p+q);
>    q = q[1];
>    y = p[q];
>    q = q[1];
>    z = q[p];
>
>Obviously this is machine dependent.

In which case, why not write it in an existing, legal, machine-dependent
fashion instead:

	long x, y, z, q, *p, a[19];
	int i;
	...
	p = a;
	q = (long)(i * sizeof(*p));
	x = *(long *)((char *)p + q);
	q += sizeof(*p);	/* I think you meant `q = &q[1]' */
	y = *(long *)((char *)p + q);
	q += sizeof(*p);
	z = *(long *)((char *)p + q);

The casts have the effect of warning the reader that something odd
is going on.  (Personally, I would rather read

	x = a[i];
	y = a[i+1];
	z = a[i+2];

than either of the two versions above.  Chances are that a decent
compiler will generate code at least as good, if not better, for
this third version.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list