Double inderection question

ark at alice.UUCP ark at alice.UUCP
Wed Aug 3 21:59:48 AEST 1988


In article <2001 at tulum.cs.swarthmore.edu>, pomeranz at swatsun.UUCP writes:

> char array[10], **ptr;
 
> main()
> {
>    ptr = &array;
>    .
>    .
>    .
> }
> 
> Now, when I try and compile this program with our C compiler (Sun OS 4.0), I
> get something like 'warning: & before array or function: ignored'.

Many C compilers treat `array' as equivalent to `&array[0]'
in this context.  Strictly speaking, a compiler that does this
ought to be entitled to treat `&array' as `&(&array[0])', which
is illegal as (the inner) & does not yield an lvalue.

However, the compiler figures that when you said &array you mean
&array[0], so it quietly translates it for you.

In any event, such a compiler treats &array as a char * and not
as a char **, so the assignment should elicit an error message.

This all changes in ANSI C.  There, &array is actually a pointer
to the array!  Its type is `pointer to array of 10 characters'
and it might be used this way:

	char array[10];
	char (*p)[10];

	p = &array;

I have it on good authority that this is valid ANSI C, and indeed
other C compilers I've tried will accept all but the last line.

The real question is: what do you want?  You sound like you want
ptr to point to another pointer, which in turn points to the
initial element of the array.  If so, the only way to do it
is to declare the intermediate pointer explicitly: C is not in
the habit of manufacturing temporary lvalues.

Incidentally, my book `C Traps and Pitfalls' discusses the
relationship between arrays and pointers in some detail.



More information about the Comp.lang.c mailing list