"array" vs. "&array" ?

Chris Torek chris at mimsy.umd.edu
Fri Dec 29 06:25:47 AEST 1989


>In article <21419 at mimsy.umd.edu> I wrote:
>>	&arr
>>produces either a warning (Classic C), or a value of type `pointer
>>to array SIZE of basetype' (New C).
>>>	p = &a;
>>Unfortunately, when handed to an Old C compiler, you get:
>>>warning: & before array or function: ignored
>>>warning: illegal pointer combination, op =

In article <1989Dec28.100415.17825 at eng.umd.edu> dskim at eng.umd.edu
(Daeshik Kim) writes:
>	If I define "char a[10];" and use " &a" (e.g. address of array a)
>	, isn't this undefined?

No.  Reread <21419 at mimsy.umd.edu>, this time without preconceived notions:

>	To my understanding, "&a" is the address of the address of the base
>	of 10 bytes mem.  The only place I can think of, is where the compiler
>	keeps the info. of allocated memory( activation record ).

No.  `a' (as defined above) is the name of an array, therefore

	&a

is

	<address-of> <object, array 10 of char, a>

Some C books (including K&R-1) would lead you to believe that anywhere
`a' occurs in C code it means `the address of the first of ten characters'.
This is false: `a' means `the variable a'.  In *value contexts* (NOT
everywhere), it is *converted to* (does not start out as) a value
that has a pointer type (namely `char *').  The operand of unary `&',
however, is NOT in a value context.  (If it were, you could ask for
`&(a + b)'.)  It is in an object context, and objects in object contexts
stay objects: arrays do not degenerate into pointers.  The `&' thus
sees the object (array 10 of char) and not the address (pointer to
a[0]) that, e.g., `char *p = a;' sees.

Again, in Old (or Classic or K&R-1) C, the unary `&' does not accept
arrays or functions, but in New (or ANSI or K&R-2) C, it does accept
arrays.  The obvious thing for `address of <object,array...>' to become
is `<value,pointer to array...>', and this is what happens (in NEW C ONLY).
-- 
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