Pointers and Arrays

Wayne Throop throopw at dg_rtp.UUCP
Sun Aug 17 05:12:57 AEST 1986


> karl at haddock (Karl W. Z. Heuer)

Ghak!!  It's a good thing I said I wouldn't be aghast if ANSI C made
&array legal, since they already *have* made it legal!  Karl raised a
point:

> Btw, someone suggested earlier that ANSI C doesn't interpret &a as pointer
> to array.  I think it does: "The operand of the unary & operator shall be
> a function locator or an lvalue [other than bit-field or register].  ...
> The result ... is a pointer to the object [and has type] `pointer to type'."
> (3.3.3.2, 01-May-1986 draft.)  Arrays are not mentioned as a special case.
> And yes, arrays *are* (non-modifiable) lvalues in X3J11.

And, looking up the references, I found in addition to the above points
a another that I don't know how I missed before:

    C.2.2.1 Except when used as an operand that may or shall be an
    lvalue, [...] an expression that has type "array of *type*" is
    converted to an expression that has type "pointer to *type*" and
    that points to the initial member of the array object.

Interestingly, the "may or shall be an lvalue" is a little strange.  I
assume that what is meant here is that the conversion is not done iff an
lvalue is required.  (After all, an lvalue *may* be used anywhere a
value may be used, but maybe I'm missing some subtle point.)  Note that
this is *different* than what K&R say.  K&R say that arrays are not
lvalues, and the only anomaly is for "sizeof" (and this anomaly is
listed along with sizeof, not with arrays).

H&S agree with K&R, (on page 97, for example):

    [array of T is converted to pointer to T].  This rule is one of the
    usual unary conversions.  The only exception to the conversion rule
    is when an array identifier is used as an operand of the sizeof
    operator,

So, when ANSI-compliant compilers hit the streets, arrays will be
lvalues, &array will be legal, and it will even behave reasonably.  They
even made the rule for array promotion reasonably simple, though the
"sizeof" is still a separate special case, and is still listed
separately.  And it's still a little difficult to get an array-typed
rvalue, so assignment still doesn't work, even aside from the fact that
ANSI doesn't make array-typed lvalues modifiable.

--
"They couldn't hit an elephant at this dist......"
        --- The last words of General John Sedgwick,
            at the battle of Spotsylvania, 1864
-- 
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list