You are on page 1of 21

CSG2A3

ALGORITMA dan STRUKTUR DATA

Introduction to Pointer and


Address
Representation of a Storage Media

2 7/26/2018
Data and Memory
Data of a variable is stored in memory
Picture it as a 1-dismension array
2

14

20

26
0

10

11

12

13

15

16

17

18

19

21

22

23

24

25

27

28

29

30

31
Each cell has a unique “index”, we call it address

3 7/26/2018
Data and Memory
While program runs, OS will allocate the memory
space for each variable
Dictionary
a : char
j : integer
arr : array[1..6]

Char a Int j Char Arr[6]


0

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31
*Just illustration

4 7/26/2018
Data and Memory
We can call or change the value of a variable by
calling the address where it’s stored
Algorithm
arr[3]  ‘b’
a  ‘u’
j  15

Char a Int j Char Arr[6]

u 1 5 b
0

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31
Byte Byte Byte
*Just illustration #6 #11-14 #23-28

5 7/26/2018
Data and Memory
Specific for C/Cpp-family programming language, we can
access the address of a variable using keyword ‘&’

Algorithm output
output( a ) u
output( &a ) 0x6
output( &Arr[3] ) 0x26

Char a Int j Char Arr[6]

u 1 5 b
0

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31
Byte Byte Byte
*Just illustration #6 #11-14 #23-28

6 7/26/2018
Pointer
Basic variable type
Store an address of a variable in hexadecimal
Size of an integer (4byte)

Char a Int j Char Arr[6]

u 1 5 b
0

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31
Byte Byte Byte
*Just illustration #6 #11-14 #23-28

7 7/26/2018
Pointer
Pointer also has a variable type
Can only points to variables of the same type
Dictionary
p_int : pointer to integer Pointer also use
p_char : pointer to char Space memory

char *p_char Char a Int j Int *p_int Char Arr[6]

u 1 5 b
3

15

21

27
0

10

11

12

13

14

16

17

18

19

20

22

23

24

25

26

28

29

30

31
*Just illustration

8 7/26/2018
Pointer (in pseudo code)
For a pointer to refer onto a variable, just assign
the variable into pointer
Use keyword * To assign the value of a variable
pointed by pointer

char *p_char Char a Int j Int *p_int Char Arr[6]

u 1 5 b
3

15

21

27
0

10

11

12

13

14

16

17

18

19

20

22

23

24

25

26

28

29

30

31
*Just illustration

9 7/26/2018
Operation using Pointer
Algorithm Output
p_int  &j
Output( j) 15
Output( p_int ) x11
Output( *p_int ) 15

char *p_char Char a Int j Int *p_int Char Arr[6]

u 1 5 x 1 1 b
1

13

19

25

31
0

10

11

12

14

15

16

17

18

20

21

22

23

24

26

27

28

29

30
10 7/26/2018
Operation using Pointer
Algorithm Output
p_char  &a
Output( *p_char ) ‘u’ // pointing to a
p_char  &arr[3]
Output( *p_char ) ‘b’ // pointing to arr[6]
a  *p_char
output( a ) ‘b’

char *p_char Char a Int j Int *p_int Char Arr[6]

x 2 6 u
b 1 5 x 1 1 b
1

13

19

25

31
0

10

11

12

14

15

16

17

18

20

21

22

23

24

26

27

28

29

30
11 7/26/2018
Pointers
On Algorithm, pointer is about the value of the
variable pointed
Here we don’t talk about how to manually set a
pointer to refer some address
Program wise, it’s also not good to manually set a
pointer into some memory address

12 7/26/2018
Don’t be confused
Dictionary
a, b : char
p1, p2 : pointer to char
Algorithm
a  ‘c’
p1  &a
p2  p1
b  *p1
*p2  ‘x’
Char b Char *p2 char *p1 Char a

c x 25 x 25 x
c
0

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31
13 7/26/2018
Don’t be confused
Dictionary 1
a, b, c, d : integer p1 a
p1, p2, p3, p4 : pointer to integer
Algorithm
a  1; b  2 2
c  3; d  4 p2 b
p1  &a; p2  &b
p3  &c; p4  &d
3
p3 c
p2  p1
*p4  *p1
p3  &b 1
4
2
*p4  b
p4 d

14 7/26/2018
Question?
Exercise – draw the pointers

Dictionary
x, y : integer
p1, p2 : pointer to integer
Algorithm p1 x
x5
y  10
x100 x200
1 p1  &x
*p1  7 p2 y
2 p2  &y
x  *p2
3 xy
p1  &y x300 x400
p2  &x
4 p2  &x
p1  p2
*p2  6

16 7/26/2018
Exercise – draw the pointers

Dictionary
x, y : integer
p1, p2 : pointer to integer
Algorithm p1 x
x5
y  10
x100 x200
1 p1  &y
p2  &x p2 y
*p1  *p2
2 p2  &x
*p2  7
p1  p2
x300 x400
3 p1  &x
*p1  y

17 7/26/2018
Exercise – write the value inside each variable and pointer

Dictionary
a, b, c : integer
p1,p2,p3 : pointer to integer
Algorithm What is the output?
a  10 a b c p1 p2 p3
b  15 10 15
p1  &b
p2  p1
c  27
p1  &c
a  *p1
p3  &b
*p2  8
18 7/26/2018
Exercise – write the value inside each variable and pointer

Dictionary
a, b, c : integer
p1, p2, p3 : pointer to integer

Algorithm
What is the output?
a  10
b  15 a b c p1 p2 p3
c  27 10 15 27

p1  &a
p2  &b
*p1  c
a  *p2
b6
p3  &b
p3  &c
*p1  *p3

19 7/26/2018
Home Task
Learn more about pointer in Cpp
Create a project to try the previous exercise in
Cpp
Read more about Dynamic Memory Allocation

20 7/26/2018
THANK YOU
7/26/2018
21