Double indirection question

Jim Vlcek vlcek at mit-caf.MIT.EDU
Thu Aug 4 01:30:45 AEST 1988


Hal Pomeranz was asking about array references and pointers to arrays,
namely, why doesn't

char array[10], **ptr;
ptr = &array;

work?  The reason is that array names are not variables, they are
compile time constants.  Hence, taking the address of an array name 
is a meaningless operation, since there is no allocated storage to
take the address of.  The proper way to think of an array name is as a
pointer-valued *constant*, not as an actual pointer.

Read page 94 of the old K&R, I don't know where it is in the new one,
for a discussion of arrays vs. pointers.

Oh, one more thing, in case you're not yet confused.  If you declare
an ``array'' in an argument list, you *do* in fact get a pointer, and
you can manipulate it as such.  For example,

foo (s) char s[];
{
        while (*s++ != '\0')
        {
        ...
        }
}

is quite correct.  This is because arrays are passed to functions as
pointers, and, due to the call-by-value nature of C function calls,
the parameter can be modified safely within the body of the called
function.  (That such a construct could not be a constant should also
be immediately clear, since the value of ``s'' above could not be
known at compile time, as it would for an actual array.)

-- 
Jim Vlcek
vlcek at caf.mit.edu
!{ihnp4,harvard,seismo,rutgers}!mit-eddie!mit-caf!vlcek



More information about the Comp.lang.c mailing list