ptrs and arrays

Chris Torek chris at mimsy.umd.edu
Fri Nov 10 15:50:22 AEST 1989


In article <0156 at sheol.UUCP> throopw at sheol.UUCP (Wayne Throop) writes:
>As is (essentially) invariably the case, the argument Chris presents
>against the usefulness of utterable array types of unknown size is
>both cogent and correct.  I think it misses a small point, however.

I did this deliberately:

>The point is controlled information hiding.

I have a (perhaps weak) argument against this particular usage.  I was
going to include it, but cannot think of any decent way to phrase it
(this is one of the reasons I cannot tell whether it is really any good).
All I can do is point out something about the wording here:

>Such types (it seems to me) are perfectly appropriate in header files,
>where the decision of actual array size can be delayed until link time
>safely,

In other words, we have something like

	extern sometype somearray[];	/* unknown size */

This much is justifiably necessary---among other things, it is the right
way to refer to variables such as `sys_siglist' and `etext' (these are
specific to Unix, but no doubt there are other examples).

>and yet provide for some operations (encapsulated in macros
>defined in the same header file preferably) on the element type of the
>array.

But look at what you just said: operations on the *element type* of the
array: not operations on the array as a whole.  Operations on these
elements need pointers to the elements, not a pointer to the array.

To put it another way (in another weak argument), I have never seen a
situation where I could justify `sometype (*p)[]', even when dealing
with someone else's array, whose type I knew, but size I did not.
Moreover, I have had to know that something was an array type, even
when its elements were hidden.  For instance, `jmp_buf' must be an
array type.  The `array-ness' of an object shows through, because only
arrays can be modified by a call that does not have an `&':

	f(x);	/* x is never modified, but x[i] could be */
	f(&x);	/* x could be modified (and x[i] could be as well) */

Arrays often get short shrift in C, and I think this applies to information
hiding as well.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list