Title: 12' USING POINTERS FOR ARRAY PROCESSING
112. USING POINTERS FOR ARRAY PROCESSING
2Pointers to Array Elements
A pointer variable may point to an element of
an array int a10, p p a0 p 5
/ stores 5 into a0 / Weve used this
ability in the past scanf("d", ai)
3Pointer Arithmetic
Three arithmetic operations are defined for
pointers Adding an integer to a
pointer Subtracting an integer from a
pointer Subtracting two pointers
4Adding an Integer to a Pointer
Adding an integer j to a pointer p yields a
pointer to the element that is j places after the
one that p points to. int a10, p, q p
a2 q p 3
5Adding an Integer to a Pointer
p 6
6Subtracting an Integer from a Pointer
If p points to the array element ai, then p -
j points to ai-j. p a8 q p -
3 p - 6
7Subtracting Pointers
When two pointers are subtracted, the result is
the distance (measured in array elements) between
the pointers. p a5 q a1 i p -
q / i is 4 / i q - p / i is -4 / Note
Performing arithmetic on a pointer p gives a
meaningful result only when p points to an array
element. Subtracting two pointers is meaningful
only when both point to elements of the same array
8Pointer Comparison
Any two pointers of the same type may be
compared using the equality operators ( and
!). Pointers may also be compared using the
relational operators, provided that they point to
elements of the same array. Example int
a10, p, q p a5 q a3 if (p lt q)
/ false /
9Using a Pointer to Step Through an Array
A pointer can be used to step through an
array define N 10 int aN, sum, p sum
0 for (p a0 p lt aN p) sum
p At the end of the first iteration
10Using a Pointer to Step Through an Array
At the end of the second iteration At the
end of the third iteration
11Using an Array Name as a Pointer
Arrays and pointers are related in the
following way The name of an array can be used
as a pointer to the first element in the
array. The first element of an array a can be
accessed by using a as a pointer int a10 a
0 / stores 0 in a0 / The second element
of a can be accessed through a 1 (a1) 1
/ stores 1 in a1 /
12Using an Array Name as a Pointer
In general, the expression ai is another way
to write ai. Using this knowledge, we can
simplify our array-summing loop define N
100 int aN, sum, p sum 0 for (p a p lt a
N p) sum p
13Array Arguments (Revisited)
When an array is used as an argument to a
function, the function is given a pointer to the
arrays first element i find_largest(b,
N) Consequently, the elements of an array
argument can be modified void store_zeroes(int
a, int n) int i for (i 0 i lt n i)
ai 0
14Array Arguments (Revisited)
To protect an array argument from change, use
the const specifier int find_largest(const int
a, int n) Because of the relationship
between arrays and pointers, an array parameter
may be declared as a pointer instead void
store_zeroes(int a, int n)
15Using a Pointer as an Array Name
When a pointer points to an array, it can be
used in place of the arrays name define N
100 int aN, i, sum, p p a sum 0 for (i
0 i lt N i) sum pi