12' USING POINTERS FOR ARRAY PROCESSING - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

12' USING POINTERS FOR ARRAY PROCESSING

Description:

When two pointers are subtracted, the result is the distance (measured in array ... Because of the relationship between arrays and pointers, an array parameter may ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 16
Provided by: Ritwik3
Learn more at: https://www.cise.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: 12' USING POINTERS FOR ARRAY PROCESSING


1
12. USING POINTERS FOR ARRAY PROCESSING
2
Pointers 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)
3
Pointer Arithmetic
Three arithmetic operations are defined for
pointers Adding an integer to a
pointer Subtracting an integer from a
pointer Subtracting two pointers
4
Adding 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
5
Adding an Integer to a Pointer
p 6
6
Subtracting 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
7
Subtracting 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
8
Pointer 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 /
9
Using 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
10
Using a Pointer to Step Through an Array
At the end of the second iteration At the
end of the third iteration
11
Using 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 /
12
Using 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
13
Array 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
14
Array 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)
15
Using 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
Write a Comment
User Comments (0)
About PowerShow.com