C'mon, guys!

Wayne Throop throopw at dg_rtp.UUCP
Sun Jun 15 04:57:37 AEST 1986


> gilbert at aimmi.UUCP (Gilbert Cockton)

> * given an array of dimensions a x b x c .. x n,
>   the array name is a pointer to array[0][0][0]..[0]

Not quite.  It isn't quite right to say that the array name "is" a
pointer... this is what causes all the confusion.  The array name, when
*evaluated*, *yields* an address (except when the object of "sizeof").
Note that here I am using the term "address" to mean a non-lvalue
address expression, and "pointer" to mean an lvalue address expression.
Using this terminology, arrays are *never* pointers (except the usually
cited kludge of an array formal argument.)

> * the only time this is any real use if when passing arrays by reference
>   as `array' is easier and safer to write than
>
>           &(array[0][0][0]..[0])
>
>   as you don't need to bear the array dimensions in mind.

Huh?  Since the operation e1[e2] is *defined* in C to be (*(e1+e2)), the
fact that an array name evaluates to the address is used *even* *when*
*an* *array* *is* *subscripted*.


I'm not sure just what you are unclear about.  It seems to me that most
people are confused on two points:

     1) They think that the fact that pointers and arrays can each be
        indirected and subscripted means that they are "the same thing".
        This is not the case.  They both yeild address values when
        evaluated (except when object of sizeof), but they are
        describing an entirely different runtime storage arrangement.
     2) If they are unconfused on point (1), they think that arrays of
        unknown size are pointers.  This is *not* the case.  This
        mistake is probably due to the fact that array formal arguments
        are equivalent to pointers, but this is a special case exception
        and does *not* make them equivalent in the general case.

If you get straight these things:

  - how pointers/arrays are *evaluated*
  - what *runtime storage* their declaration implies
  - what *operations* can be done on addresses, and how subscripting is
    defined in terms of address arithmetic

then you have a chance of understanding arrays and pointers in C.  Most
people are confused on one or more of these points.
-- 
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list