Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!uxc!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: When is a cast not a cast? Message-ID: <17739@mimsy.UUCP> Date: 25 May 89 03:36:23 GMT References: <2747@buengc.BU.EDU> <6200006@ux1.cso.uiuc.edu> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 42 In article <6200006@ux1.cso.uiuc.edu> phil@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@mimsy.umd.edu Path: uunet!mimsy!chris