Double inderection question

Guy Harris guy at gorodish.Sun.COM
Wed Aug 3 14:01:30 AEST 1988


> Consider the following:
> 
> char array[10], **ptr;
> 
> main()
> {
>    ptr = &array;
>    .
>    .
>    .
> }
> 
> Now, when I try and compile this program with our C compiler (Sun OS 4.0),

Or most, if not all, other PCC-based compilers....

> I get something like 'warning: & before array or function: ignored'.  I've
> also tried '&&array[0]', '&&(array[0])', '&(&array[0])', etc. and none of
> these has worked.  From my limited understanding 'array' is a pointer to the
> first element (i.e. a pointer to array[0]), so &array should be a pointer to
> the pointer.

No.

"array" is a pointer-valued expression that points to array[0].  It is not a
pointer variable, or other pointer object, so you can't construct a pointer to
it - there wouldn't be anything for it to point to.

Consider:

	int *i;

	i = &666;

The compiler will not find that construct at all amusing, since there's no
object to which "666" refers (no, there's no guarantee that the compiler
allocates a data location to hold the 666).  "&array" is a similar case.

Now, in the language specified by recent drafts of ANSI C, "array" does not
refer to "&array[0]" in certain contexts.  One of those contexts is that of the
operand of "&"; thus, "&array", in ANSI C (a shorthand for "the language
specified by recent drafts of ANSI C"), is a pointer to the array named
"array".  In your example, "&array" would have type "pointer to array of 10
'char's".  It would, in most implementations, evaluate to a pointer with the
same bit pattern as "&array[0]".

> As a side point, the following does work:
> 
> char array[10], **ptr, *bogus_ptr;
> 
> main()
> { 
>    bogus_ptr = array;
>    ptr = &bogus_ptr;
>    .
>    .
>    .
> }
> 
> It strikes me that if I can do what I want in this roundabout way, I should
> also be able to do it directly.

Nope.  Nothing roundabout there.  Consider:

	int *i, iii;

	iii = 666;
	i = &iii;

If you want a pointer to an object that is a pointer to "array", you first have
to construct that object....



More information about the Comp.lang.c mailing list