Koenig on pointers and arrays

Andrew Koenig ark at alice.UUCP
Fri Jan 19 02:58:16 AEST 1990


In article <2804 at bingvaxu.cc.binghamton.edu>, cjoslyn at bingvaxu.cc.binghamton.edu (Cliff Joslyn) writes:

> "Only two things can be done to an array: determine its size and obtain
> a pointer to element 0 of the array.  *All* other array operations are
> actually done with pointers, even if they are written with what look
> like subscripts.  That is, every subscript operation is equivalent to a
> pointer operation, so it is possible to define the behavior of
> subscripts entirely in terms of the behavior of pointers".

> I presume this is accurate, and that e.g.  passing an array as a
> parameter or taking its address are considered array operations which
> are actually done with pointers.  In other words, is the first sentence
> above literally accurate?

On reading this sentence again, I still believe that it is literally
accurate.  For example:

	void f(int*);
	int a[10];

	main()
	{
		f(a);
	}

A casual way to think of this is that you're passing the array `a'
to the function `f'.

What's actually happening, though, is that the mere mention of the
array `a' immediately yields a value that is the address of the
initial (0th) element of `a'.  Thus the call

	f(a);

is precisely equivalent to the call

	f(&(a[0]));

in which it is perhaps more readily visible that what is being passed
is actually the address of an element.

If I really wanted to split hairs, I could have added that you can
take the address of an array (but only in an ANSI compiler).  That
would have confused the main point, though.  I do explicitly take
the address of an array on page 29 and again several times on page 31.
I don't think of taking the address of an array as being an
array operation, though.  There's nothing special about the fact
that it's an array.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list