Programming in C

(Barry) #1

260 Chapter 11 Pointers


When you define a pointer that is used to point to the elements of an array, you don’t
designate the pointer as type “pointer to array”; rather, you designate the pointer as
pointing to the type of element that is contained in the array.
If you have an array of characters called text,you could similarly define a pointer to
be used to point to elements in textwith the statement
char *textPtr;
To set valuesPtrto point to the first element in the valuesarray, you simply write
valuesPtr = values;
The address operator is not used in this case because the C compiler treats the appear-
ance of an array name without a subscript as a pointer to the array.Therefore, simply
specifying valueswithout a subscript has the effect of producing a pointer to the first
element of values(see Figure 11.9).

values[0]
values[1]
values[2]
values[3]

valuesPtr

values[99]

Figure 11.9 Pointer to an array element.

An equivalent way of producing a pointer to the start of valuesis to apply the address
operator to the first element of the array.Thus, the statement
valuesPtr = &values[0];
can be used to serve the same purpose as placing a pointer to the first element of
valuesin the pointer variable valuesPtr.
Free download pdf