Indefinite-length array as member of struct: how?

Chris Torek chris at mimsy.UUCP
Wed Jul 12 04:29:53 AEST 1989


In article <321 at yetti.UUCP> asst-jos at yetti.UUCP (Jonathan) writes:
>Remember that although by definition, the name of an array is a pointer
>to the array, there are certain limitations.

The name of an array is *not* a pointer to the array.  Forget `array
equals pointer'; it is false.  In an rvalue context, an object of type
`array N of T' is converted to one of type `pointer to T'; its value
is the address of the 0'th (first) element of the array (&array[0]).

>... namely
>
>	char string[SIZE];
>
>	*string = ....
>
>is invalid. 

Apply the rule: `In an rvalue context'---do we have an rvalue context?
The context here in which the object of type `array SIZE of char'
appears is `*string'.  Looking up the description for `*' in any good C
book, we find that it means either multipy (infix) or indirect
(prefix).  Here it is being used as the latter.  The target of the `*'
(here `string') is indeed in an rvalue context.  Thus, `string' is
converted from an object of type `array SIZE of char' to one of type
`pointer to char'.  Its value is the address of `string[0]'
(&string[0]).  The details of the indirection operator are that it
converts a pointer into the object to which that pointer points (here
string[0]), and yeilds an lvalue (so that we can assign into
string[0]).  Thus,

	char string[SIZE];

	*string = <any integer expression>;

is legal and means `write the value produced by the integer expression
into string[0]'.

Arrays are not pointers; pointers are not arrays.  There is a symmetry
between the two, but they are different.  The real relationship is that
array object are sometimes converted into pointers, and that pointers
that point to one member of an array may be used to reach any member of
that array.  If you want an analogy, they are like matter and energy,
interconvertible (E = mc^2) but not identical (you would eat an apple,
but you would not eat 7600000 gigajoules of energy [~3 oz]).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list