What does Z["ack"] = 5 mean?

Andrew Koenig ark at alice.UUCP
Sun Oct 9 12:36:35 AEST 1988


In article <4700019 at m.cs.uiuc.edu>, kenny at m.cs.uiuc.edu writes:
> 
> /* Written  2:54 pm  Oct  5, 1988 by merlyn at intelob.intel.com in m.cs.uiuc.edu:comp.lang.c */
> Now, you may not like the idea of the subscript transposed with the
> array name, but it is legal C.
> /* End of text from m.cs.uiuc.edu:comp.lang.c */
> 
> Since K&R are somewhat unclear on the point, some compiler
> implementors, notably Encore, have been lax about implementing
> integer[pointer] and integer +- pointer.

First edition, page 186:

	A primary expression followed by an expression in
	square brackets is a primary expression.  The intuitive
	meaning is that of a subscript.  Usually, the primary
	expression has type ``pointer to ...'', the subscript
	expression is int, and the type of the result is ``...''.
	The expression E1[E2] is identical (by definition)
	to *((E1)+(E2)).  All the clues needed to understand
	this notation are contained in this section together
	with the discussion in sections 7.1, 7.2, and 7.4
	on identifiers, *, and + respectively; section 14.3 below
	summarizes the implications.

And on page 210, in section 14.3:

	Because of the conversion rules which apply to +, if E1
	is an array and E2 an integer, then E1[E2] refers to the
	E2-th member of E1.  Therefore, despite its asymmetric
	appearance, subscripting is a commutative operation.

I don't see how it's possible to be more explicit than that.
E1[E2] means precisely the same thing as *((E1)+(E2)) and is
therefore meaningful in every context where *((E1)+(E2)) is
meaningful.  In particular, it means the same as E2[E1].
I can't see how to draw any other conclusion from the above.

Indeed, some compiler writers may get it wrong.  But don't blame K&R.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list