Programming in C

(Barry) #1

264 Chapter 11 Pointers


expression *ptris used inside the forloop to access the elements in the array. Formerly,
you would have written your arraySumfunction with a forloop that used an index
variable, such as i, and then would have added the value of array[i]into suminside the
loop. In general, the process of indexing an array takes more time to execute than does
the process of accessing the contents of a pointer. In fact, this is one of the main reasons
why pointers are used to access the elements of an array—the code that is generated is
generally more efficient. Of course, if access to the array is not generally sequential,
pointers accomplish nothing, as far as this issue is concerned, because the expression
*(pointer + j)takes just as long to execute as does the expression array[j].

Is It an Array or Is It a Pointer?


Recall that to pass an array to a function, you simply specify the name of the array, as
you did previously with the call to the arraySumfunction.You should also remember
that to produce a pointer to an array, you need only specify the name of the array.This
implies that in the call to the arraySumfunction, what was passed to the function was
actually a pointerto the array values.This is precisely the case and explains why you are
able to change the elements of an array from within a function.
But if it is indeed the case that a pointer to the array is passed to the function, then
you might wonder why the formal parameter inside the function isn’t declared to be a
pointer. In other words, in the declaration of arrayin the arraySumfunction, why isn’t
the declaration
int *array;
used? Shouldn’t all references to an array from within a function be made using pointer
variables?
To answer these questions, recall the previous discussion about pointers and arrays. As
mentioned, if valuesPtrpoints to the same type of element as contained in an array
called values, the expression *(valuesPtr + i)is in all ways equivalent to the expres-
sion values[i], assuming that valuesPtrhas been set to point to the beginning of
values.What follows from this is that you also can use the expression *(values + i)
to reference the ith element of the array values, and, in general, if xis an array of any
type, the expression x[i]can always be equivalently expressed in C as *(x + i).
As you can see, pointers and arrays are intimately related in C, and this is why you
can declare arrayto be of type “array of ints” inside the arraySumfunction orto be of
type “pointer to int.” Either declaration works just fine in the preceding program—try
it and see.
If you are going to be using index numbers to reference the elements of an array that
is passed to a function, declare the corresponding formal parameter to be an array.This
more correctly reflects the use of the array by the function. Similarly, if you are using the
argument as a pointer to the array, declare it to be of type pointer.
Realizing now that you could have declared arrayto be an intpointer in the pre-
ceding program example, and then could have subsequently used it as such, you can
Free download pdf