Sunteți pe pagina 1din 12

UCCD1024

Data Structures And


Algorithmic Problem Solving

Pointers Use With Arrays


What you will learn?

 Pointer seen as 1-dimensional array


 Pointer dereferencing

L04: Pointer interpretation 2


Pointer seen as 1-dimensional array
 Rules to remember.
 Given a pointer p (defined as “char *p;” or “int p[6];” as
shown below),
 We can always see or treat p as representing a 1-
dimensional array with p storing the address (arrow)
pointing to an array object of size 1 or many as shown
below.

(pointer / reference)
p a

p 20 -1 5 6 99 -2

L04: Pointer interpretation 3


Pointer seen as 1-dimensional array
 Given a pointer p or q,
 p±n or q±n is interpreted by C++ as another pointer
value pointing to an element n positions away from
current element.
 The arrows below represent different values of p±n
(q±n) with p defined as “char *p” and q as “int q[6]”.
 C++ will add the correct amount of bytes to p or q
according to the number of bytes to represent a
char or int.
(pointer / reference)
p a
p+0 p+1 p+10

q 20 -1 5 6 99 -2

L04: Pointer interpretation q+1 q+4 q+6 4


Pointer seen as 1-dimensional array
 For example, if p is 123 and ‘char’ needs only 1
bytes, C++ will translate ‘p+1’ to be 124, ‘p+10’ to
be 133.
 As another example, if q is 400 and ‘int’ needs 4
bytes, C++ will translate ‘q+1’ to 404 and ‘q+4’ to
416.

(pointer / reference)
p a
p+0 p+1 p+10

q 20 -1 5 6 99 -2

L04: Pointer interpretation q+1 q+4 q+6 5


What you will learn?
 Pointer seen as 1-dimensional array

 Pointer dereferencing

L04: Pointer interpretation 6


Pointer dereferencing (*p)
 Given a pointer p, the dereference operator ‘*’ utilized as ‘*p’
allows us to access the memory content pointed by p.
 The goal of ‘*p’ may be to retrieve or change the value stored
at such location. See examples below.
 Given p as “char p[6];” as shown below, the expressions of x
= *p, x = *(p+1), x = *(p+4), x = *(p+10) will change x to
__________________________________________________
a b e ? respectively

(pointer / reference)
p a b c d e f

L04: Pointer interpretation 7


Pointer dereferencing (*p)
 Given a pointer p, the dereference operator ‘*’ utilized as ‘*p’
allows us to access the memory content pointed by p.
 The goal of ‘*p’ may be to retrieve or change the value stored
at such location. See examples below.
 Given p as “char p[6];” as shown below, the expressions of x
= *p, x = *(p+1), x = *(p+4), x = *(p+10) will change x to
a b e ? respectively
__________________________________________________
 Similarly, the expressions of *p = ‘z’, *(p+1)=‘z’, *(p+4) = ‘z’,
*(p+10) = ‘z’ will change the memory cells below to

(pointer / reference)
p za bz c d z
e f z
This is bad! 8
L04: Pointer interpretation
Pointer dereferencing (*p)

 Equivalently, one can treat p as pointing to a 1D array


and use p[n] to access the same location.
 The previous operations can be rewritten as
 x=p[0], x=p[1], x=p[4], x=p[10]
 p[0]=‘z’, p[1]=‘z’, p[4]=‘z’, p[10]=‘z’

(pointer / reference)
p a b c d e f

L04: Pointer interpretation 9


Pointer dereferencing

 Generalization. Given a pointer p (defined as “char *******p;”),


 **p: represents the content of the memory cell that is 2
arrows away from p, i.e. one “*” represents one arrow.
Hence, **p corresponds to which colored box ?
blue
_____________
 ***p: represents the memory content 3 arrows away. This
green
is corresponding to which colored box ? __________

p a

L04: Pointer interpretation 10


Summary
 Pointer and 1-D array, i.e. a pointer always treats its content
as address of a very long 1-D array.
 The concept of pointer dereferencing operator ‘*’: *ptr
represents the memory content one arrow away from ptr. **ptr
represents the memory content 2 arrows away from ptr, etc.
 If p or q is pointing at an element, p+1 or q+1 represents the
address of the next element, p+n represents the address of
the element n positions away from the first one.

(pointer / reference)
p a
p+0 p+1 p+10

q 20 -1 5 6 99 -2

L04: Pointer interpretation q+1 q+4 q+6 11


Next lecture

 Class: our understanding of object oriented programming


concept.

L04: Pointer interpretation 12

S-ar putea să vă placă și