Another silly question

Guy Harris guy at auspex.auspex.com
Sat May 20 14:49:25 AEST 1989


>Can you explain why compilers produce different code for "a[i]" and "*(a+i)"?

If the compiler doesn't produce equally good code for those two cases,
it's because the compiler writer wasn't doing as good a job as s/he
could.  If it produces different but equally good code, I dunno;
possibly because the compiler writer didn't understand that "a[i]" is
equivalent to "*(a+i)", or decided for whatever reason to implement them
differently.

The existence of compilers that produce different code for those cases
does not, in any way, prove that the two expressions are in equivalent;
K&R First Edition points out that

	...The expression E1[E2] is identical (by definition) to
	*((E1)+(E2)).

and the December 7, 1988 ANSI C draft says that

	...The definition of the subscript operator [] is that E1[E2] is
	identical to *(E1+(E2))).

so further discussion on whether they're equivalent in C is pointless -
they are, and that's that.  If somebody wants to debate whether they
*should* be equivalent, they can, but they're then talking about D or P,
say, not C. 



More information about the Comp.lang.c mailing list