Expert C Programming

(Jeff_L) #1

program. That decision has been implicitly revoked by the enhanced error checking done by most
ANSI C compilers.


The array/pointer equivalence for parameters was done for efficiency. All non-array data arguments in
C are passed "by value" (a copy of the argument is made and passed to the called function; the
function cannot change the value of the actual variable used as an argument, just the value of the copy
it has). However, it is potentially very expensive in memory and time to copy an array; and most of
the time, you don't actually want a copy of the array, you just want to indicate to the function which
particular array you are interested in at the moment. One way to do this might be to allow parameters
to have a storage specifier that says whether it is passed by value or by reference, as occurs in Pascal.
It simplifies the compiler if the convention is adopted that all arrays are passed as a pointer to the start,
and everything else is passed by making a copy of it. Similarly the return value of a function can never
be an array or function, only a pointer to an array or function.


Some people like to think of this as meaning that all C arguments are call-by-value by default, except
arrays and functions; these are passed as call-by-reference parameters. Data can be explicitly passed as
call-by-reference by using the "address of" operator. This causes the address of the argument to be
sent, rather than a copy of the argument. In fact, a major use of the address-of operator & is simulating
call-by-reference. This "by reference" viewpoint isn't strictly accurate, because the implementation
mechanism is explicit—in the called procedure you still only have a pointer to something, and not the
thing itself. This makes a difference if you take its size or copy it.


How an Array Parameter Is Referenced


Figure 9-3 shows the steps involved in accessing a subscripted array parameter.


Figure 9-3. How a Subscripted Array Parameter Is Referenced

Note well that this is identical to Diagram C on page 101, showing how a subscripted pointer is looked
up. The C language permits the programmer to declare and refer to this parameter as either an array
(what the programmer intends to pass to the function) or as a pointer (what the function actually gets).
The compiler knows that whenever a formal parameter is declared as an array, inside the function it
will in fact always be dealing with a pointer to the first element of an array of unknown size. Thus, it
can generate the correct code, and does not need to distinguish between cases.


No matter which of these forms the programmer writes, the function doesn't automatically know how
many elements there are in the pointed-to thing. There has to be some convention, such as a NUL end
marker or an additional parameter giving the array extent. This is not true in, for example, Ada, where

Free download pdf