Address of array

Chris Torek chris at umcp-cs.UUCP
Fri Mar 21 22:04:26 AEST 1986


In article <58 at paisley.ac.uk> rh at cs.paisley.ac.uk (Robert Hamilton)
takes exception to something in article <211 at dg_rtp.UUCP> by
throopw at dg_rtp.UUCP (Wayne Throop), who writes:

>>I believe that to support reasonable portable code C *must* allow
>>the address operator on an array, even if it is not required.

Robert says:

>Taking the address of an array just doesnt make sense in C.

I think you missed the point.  Suppose you write the following:

	#include "../projlib/types.h"

	x_type	variable;

	f()
	{
		g(&variable);
		...
	}

	g(p)
		x_type *p;
	{
		x_type newvalue;

		...
		*p = newvalue;
		...
	}

This looks perfectly reasonable, and works quite well if `x_type'
is a name for a simple type, a structure, or a union.  It does not
work---indeed, it does not even compile---if `x_type' is a name
for an array.  The problem is that you are not `allowed' to know
just what `x_type' really is.

As it turns out, it is not often useful to write something like
the above if `x_type' is a name for an array, and this problem does
not seem to come up in practice---or at least I have not seen it.
And if all else fails one can always wrap the array in a structure:

	typedef struct {
		int	x_val[10];
	} x_type;

This does seem a bit of a kludge, though.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list