Another silly question

Mark A Terribile mat at mole-end.UUCP
Fri Apr 28 18:54:12 AEST 1989


> ...  I have a text book which says that
> 	*(a + i)	and	a[i]
> are equivalent, given an array a, and int index i ... each gives the
> value stored in a[i].  But he says that
> 	*(a + i)
> is non-standard and would not expect it do go far on all _real_ C compilers
> ... He expects that many compilers would instead add the value of i to the
> pointer a, and then reference the item stored there.  I say that the
> compiler's smart enough to realize what we're trying to achieve, and
> won't do something like
> 
> 	* (char *) ( (int) a + i )
> 
> which he thinks it will probably do on most machines. ...

Oy vey!  Of course they are equivalent; that is how subscripting is *defined*
in C.  Further, any compiler that introduces the effect of spurious type
conversions of pointer expressions is broken.

And I can testify of my own knowledge that at least two C compiler families
with which some or most of us have experience transform the parser tree for the
subscripted expression into the parse tree for explicit indirection before
trying to generate code.  If THAT won't cause them to produce the same code
for both forms, there's very little that will.

K&R state quite literally that the two expressions are identical, and
further that

	a[ i ]

is the same as

	i[ i ]

I verified it on the PDP-11 compiler and on a Z-80 compiler derived from the
PDP-11 compiler; I also tried it on an early PCC.  I haven't tried it lately
on anything.  Why don't you try it on your favorite machine?  K&R say it should
work!
-- 

(This man's opinions are his own.)
>From mole-end				Mark Terribile



More information about the Comp.lang.c mailing list