Address of array

Robert Hamilton rh at cs.paisley.ac.uk
Tue Apr 1 23:37:51 AEST 1986


In article <260 at peregrine.UUCP> mike at peregrine.UUCP (Mike Wexler) writes:
>In article <58 at paisley.ac.uk> rh at cs.paisley.ac.uk (Robert Hamilton) writes:
>>
>>Taking the address of an array just doesnt make sense in C.
>>You can see the reason if you know why the following bit of
>>code also won't work:
>>int a[10];
>>int *b;
>>a=b; /* makes no sense */
>true
>>a+=1; /* ditto */
>true
>>b=a; /* ok of course */
>>
>>The decl. a[10] does 2 things:
>> 1. reserves storage for 10 elements
>true
>> 2. lets the compiler know that "a" is an int * to
>>    of the first element reserved.
>>    It does *not* reserve storage for a pointer to the storage.
>true
>Declarations never reserve storage for a pointer to the storage.  Your argument
>doesn't make sense.
>It would be useful to be able to apply the ampersand operator to any object
>without knowing what its "real" type is(it could be hiddenn by typedefs). 
>
>>So "&a" only exists during compilation, in the sense that the
>>compiler holds the address of the reserved storage somewhere
>>that "somewhere" has an address at compile time.
>>The decl. int *b on the other hand does 2 different things.
>>1. lets the compiler know that "b" is an int * ( for pointer arithmetic)
>true
>>2. and reserves a storage location for b.
>>   so &b does exist at run time.
>wrong.  This seems to be your point of confusion.  "int *b" reserves space for
>b that is correct. "b" is a pointer. "&b" is a pointer to that pointer.  They
>are not the same.

NO you misunderstand me.
My argument is a valid explanation of why C rejects the address of an array
(at least in my interpretation of what EXISTS means)
If storage is reserved for b then that storage must have a runtime address
must it not ?? And so that address is &b and can be taken as rhs.
You say so yourself :
>int *pi;
>int **pi=π /**/

I don't of course mean that &b is some value sitting somewhere in memory
so you cant take the &b as a lhs .(as you say)
In the same way (int a[10]) sits in memory somewhere and has an address a
so you can say int *b=a which also happens to be &a[0].
But a itself does not sit in memory (as b does) and so &a does not exist.

The pointer/array confusion comes because
C takes a to be the address of the array rather than the array itself,
which is inconsistent with the treatment of everything else.

But enough of this, I do see the problem and agree...
-- 
UUCP:	...!seismo!mcvax!ukc!paisley!rh
DARPA:	rh%cs.paisley.ac.uk		| Post: Paisley College
JANET:	rh at uk.ac.paisley.cs		|	Department of Computing,
Phone:	+44 41 887 1241 Ext. 219	|	High St. Paisley.
					|	Scotland.
					|	PA1 2BE



More information about the Comp.lang.c mailing list