Data Structure - PowerPoint PPT Presentation

1 / 114
About This Presentation
Title:

Data Structure

Description:

Title: Struktur Data Author: Mohd Razak Samingan Last modified by: rbk Created Date: 8/22/2006 11:28:49 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 115
Provided by: MohdRazak
Category:

less

Transcript and Presenter's Notes

Title: Data Structure


1
Data Structure Algorithm
  • Pointer

2
Introduction to Pointer
include ltiostreamgt using namespace std int
main() int i 10 cout ltlt i ltlt "\n" cout
ltlt i ltlt "\n" return 0
  • As a starting point, consider the simple C code
    above

3
Introduction to Pointer
include ltiostreamgt using namespace std int
main() int i 10 cout ltlt i ltlt "\n" cout
ltlt i ltlt "\n" return 0
  • The statement int i 10 is used to define one
    variable named i with type of integer and value
    is 10

4
Introduction to Pointer
include ltiostreamgt using namespace std int
main() int i 10 cout ltlt i ltlt "\n" cout
ltlt i ltlt "\n" return 0
  • At this point we might agree that there are only
    two entities exist with that single statement
    which are i and its value 10

5
Introduction to Pointer
include ltiostreamgt using namespace std int
main() int i 10 cout ltlt i ltlt "\n" cout
ltlt i ltlt "\n" return 0
  • Below is the possible output of the program10
  • 0xfeed4d84

6
Introduction to Pointer
include ltiostreamgt using namespace std int
main() int i 10 cout ltlt i ltlt "\n" cout
ltlt i ltlt "\n" return 0
  • From the output we know that there are other
    entity which is 0xfeed4d84

7
Introduction to Pointer
include ltiostreamgt using namespace std int
main() int i 10 cout ltlt i ltlt "\n" cout
ltlt i ltlt "\n" return 0
  • Right now we should know that there are three
    entities exist with that single statement which
    are i, 10 and 0xfeed4d84

8
Introduction to Pointer
  • To better understand this situation we could try
    to imagine that the computer main memory (RAM)
    may could be represented as a table below

Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfeed4d84 10
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
9
Introduction to Pointer
  • Each time variable is defined it will be set by
    the system to refer to some address in the
    computer main memory

Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfeed4d84 10
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
10
Introduction to Pointer
  • We can conclude that variable i is used to refer
    to the address 0xfeed4d84 in the main memory
    which store the value of integer 10

Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfeed4d84 10
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
11
Introduction to Pointer
  • This is the nature for most of programming
    languages where its easy to get back the value
    of 10 from the memory by remembering i instead
    of 0xfeed4d84

Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfeed4d84 10
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
12
Introduction to Pointer
  • Base on our new level of understanding we might
    want to refine the previous simple C code as
    below

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" return 0
13
Introduction to Pointer
  • Also note that i is a syntax if we want to get
    the address referred by the variable i and its
    also applied to other type of variables (char,
    float, double)

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" return 0
14
Introduction to Pointer
  • Before go further you should try to make your own
  • conclusion by answering the following questions
  • What are variable, address, value and memory?
  • How to get the address of particular defined
    variables?

15
Introduction to Pointer
  • Base on the C code below we know that we can
    print out the address of i

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" return 0
16
Introduction to Pointer
  • Can we store the address of i into other
    variable?

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" return 0
17
Introduction to Pointer
  • You could try by adding a few lines of code
    below.

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i return 0
18
Introduction to Pointer
  • At the compilation time (linux) you might get the
    errorerror invalid conversion from int to int

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i return 0
19
Introduction to Pointer
  • It is due to the fact that variable p is declared
    to store the value of integer and not the address
    of memory that store integer value

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i return 0
20
Introduction to Pointer
  • To declare p so it can store the address of
    memory that store integer value just change the
    statement from int p to int p

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i return 0
21
Introduction to Pointer
  • To declare p so it can store the address of
    memory that store integer value just change the
    statement from int p to int p

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i return 0
22
Introduction to Pointer
  • Print out the information for p by using the
    pattern we print out the information for i

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i return 0
23
Introduction to Pointer
  • Try print out the information of p by using the
    pattern we print out the information of i

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i cout ltlt Value
of p ltlt p ltlt "\n" cout ltlt Address of p
ltlt p ltlt "\n" return 0
24
Introduction to Pointer
  • The possible output may look like below

Value of i 10 Address of i 0xbfe35f9c
Value of p 0xbfe35f9c Address of p 0xbfe35f98
25
Introduction to Pointer
  • Notice that Address of i and Value of p got the
    same number which is 0xbfe35f9c

Value of i 10 Address of i 0xbfe35f9c
Value of p 0xbfe35f9c Address of p 0xbfe35f98
26
Introduction to Pointer
  • To get a better understanding, create a table
    that representing the current state of main
    memory for this situation

Value of i 10 Address of i 0xbfe35f9c
Value of p 0xbfe35f9c Address of p 0xbfe35f98
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 10
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
27
Introduction to Pointer
  • i is an integer and p is a pointer that store the
    address referred by i

Value of i 10 Address of i 0xbfe35f9c
Value of p 0xbfe35f9c Address of p 0xbfe35f98
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 10
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
28
Introduction to Pointer
  • Its possible to access the value of i via p

cout ltlt Value of i via p is ltlt p ltlt
"\n"
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 10
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
29
Introduction to Pointer
  • This is what we call pointer dereferencing as it
    will refer to the address stored by the pointer

cout ltlt Value of i via p is ltlt p ltlt
"\n"
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 10
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
30
Introduction to Pointer
  • This is what we call pointer dereferencing as it
    will refer to the address stored by the pointer

cout ltlt Value of i via p is ltlt p ltlt
"\n"
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 10
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
31
Introduction to Pointer
  • Its also possible to change the value of i by
    dereferencing p

p 20 cout ltlt Value of i after p
20 is ltlt i ltlt "\n"
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 10
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
32
Introduction to Pointer
  • Its also possible to change the value of i by
    dereferencing p

p 20 cout ltlt Value of i after p
20 is ltlt i ltlt "\n"
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 10
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
33
Introduction to Pointer
  • Its also possible to change the value of i by
    dereferencing p

p 20 stdcout ltlt Value of i after
p 20 is ltlt i ltlt "\n"
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 10
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
34
Introduction to Pointer
  • Its also possible to change the value of i by
    dereferencing p

p 20 stdcout ltlt Value of i after
p 20 is ltlt i ltlt "\n"
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
35
Introduction to Pointer
  • Its also possible to change the value of i by
    dereferencing p

p 20 stdcout ltlt Value of i after
p 20 is ltlt i ltlt "\n"
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
36
Introduction to Pointer
  • The program that we should have so far to
    understand the pointer

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i cout ltlt Value
of p ltlt p ltlt "\n"cout ltlt Address of p
ltlt p ltlt "\n" cout ltlt Value of i via p is
ltlt p ltlt "\n" p 20 cout ltlt Value
of i after p 20 is ltlt i ltlt
"\n" return 0
37
Introduction to Pointer
  • Notice that after i and p are defined they are
    called with different approaches/manners which
    are i, i, p, p, and p

include ltiostreamgt using namespace std int
main() int i 10 cout ltlt Value of i
ltlt i ltlt "\n" cout ltlt Address of i ltlt i
ltlt "\n" int p p i cout ltlt Value
of p ltlt p ltlt "\n"cout ltlt Address of p
ltlt p ltlt "\n" stdcout ltlt Value of i via
p is ltlt p ltlt "\n" p 20 stdcout ltlt
Value of i after p 20 is ltlt i ltlt
"\n" return 0
38
Introduction to Pointer
  • Using the table of memory below we can visualize
    what i, i, p, p, and p are all about

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
39
Introduction to Pointer
  • There is no i as i is not a pointer

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
40
Introduction to Pointer
  • The pointer p can also be assigned with the
    memory address allocated without using any
    defined and named variable such as i

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
41
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as follows

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
42
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
43
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
. . . . . . . . . . . .
. . . . . . . . . . . .
44
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
45
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
46
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
47
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f9c
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
48
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
49
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
50
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new int

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
51
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new intp 30

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
52
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new intp 30

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? ???
. . . . . . . . . . . .
53
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new intp 30

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? 30
. . . . . . . . . . . .
54
Introduction to Pointer
  • This technique referred as a dynamic memory
    allocation and can be applied as followsp
    new intp 30

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? 30
. . . . . . . . . . . .
55
Introduction to Pointer
  • Beware that the address 0xbfe35f?? is allocated
    using dynamic memory allocation so it doesnt
    have any defined variable referring to it
    (variable name assign with ???)

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? 30
. . . . . . . . . . . .
56
Introduction to Pointer
  • If the program stop right now, the operating
    system aware and able to release/freeing the
    memory space at 0xbfe35f9c and 0xbfe35f98 as it
    was formally defined and named with i and p

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? 30
. . . . . . . . . . . .
57
Introduction to Pointer
  • Its not the same for 0xbfe35f?? as it was
    allocated using dynamic memory allocation and
    require the programmer manually release/freeing
    it

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? 30
. . . . . . . . . . . .
58
Introduction to Pointer
  • It can be done by using delete function and set p
    to NULL as below each time before the program
    exit

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? 30
. . . . . . . . . . . .
59
Introduction to Pointer
  • It can be done by using delete function and set p
    to NULL as below each time before the program
    exit delete p

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
??? int 0xbfe35f?? 30
. . . . . . . . . . . .
60
Introduction to Pointer
  • It can be done by using delete function and set p
    to NULL as below each time before the program
    exitdelete p

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
. . . int 0xbfe35f?? 0
. . . . . . . . . . . .
61
Introduction to Pointer
  • It can be done by using delete function and set p
    to NULL as below each time before the program
    exit delete pp NULL

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 0xbfe35f??
. . . int 0xbfe35f?? 0
. . . . . . . . . . . .
62
Introduction to Pointer
  • It can be done by using delete function and set p
    to NULL as below each time before the program
    exitdelete pp NULL

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c 20
p int 0xbfe35f98 NULL
. . . int 0xbfe35f?? 0
. . . . . . . . . . . .
63
Introduction to Pointer
  • Is the current situation below is valid or
    allowed?

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c ???
p int 0xbfe35f98 0xbfe35f??
f float 0xbfe35f?? ???
. . . . . . . . . . . .
64
Introduction to Pointer
  • It is not valid or allowed as p is defined to
    only hold the address of integer while 0xbfe35f??
    is the address of float

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c ???
p int 0xbfe35f98 0xbfe35f??
f float 0xbfe35f?? ???
. . . . . . . . . . . .
65
Introduction to Pointer
  • This situation can be experimented by adding the
    following lines of code and will get an error at
    compilation timefloat f 1.7p f

p
p
i
p
i
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xbfe35f9c ???
p int 0xbfe35f98 0xbfe35f??
f float 0xbfe35f?? ???
. . . . . . . . . . . .
66
Introduction to Pointer
  • In certain circumstances it also possible to have
    two pointer that point to the same address as
    below

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
67
Introduction to Pointer
  • In certain circumstances it also possible to have
    two pointer that point to the same address as
    belowint p2

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
68
Introduction to Pointer
  • In certain circumstances it also possible to have
    two pointer that point to the same address as
    belowint p2

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
p2 int 0xbfe35f?? . . .
69
Introduction to Pointer
  • In certain circumstances it also possible to have
    two pointer that point to the same address as
    belowint p2 p2 p

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
p2 int 0xbfe35f?? . . .
70
Introduction to Pointer
  • In certain circumstances it also possible to have
    two pointer that point to the same address as
    belowint p2 p2 p

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
p2 int 0xbfe35f?? 0xfe35f9c
71
Introduction to Pointer
  • In certain circumstances it also possible to have
    two pointer that point to the same address as
    belowint p2 p2 p

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
p2 int 0xbfe35f?? 0xfe35f9c
p2
72
Introduction to Pointer
  • Using the memory of table like below might help
    us to better understand the pointer but its not
    practical for complex data type such as class

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
73
Introduction to Pointer
  • We can simplify it as follows

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
74
Introduction to Pointer
  • We can simplify it as follows

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
75
Introduction to Pointer
  • We can simplify it as follows

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
i
20
76
Introduction to Pointer
  • We can simplify it as follows

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
i
20
77
Introduction to Pointer
  • We can simplify it as follows

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
i
20
p
.
78
Introduction to Pointer
  • We can simplify it as follows

p
Variable Name Type Address Value
. . . . . . . . . . . .
i int 0xfe35f9c 20
p int 0xbfe35f98 0xfe35f9c
. . . . . . . . . . . .
i
20
p
.
79
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

80
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

81
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
p2
.
temp
.
82
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
p2
.
temp
.
83
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
temp
.
84
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
temp
.
85
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
temp
.
86
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
temp
.
87
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
temp
.
88
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
???
temp
.
89
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
???
temp
.
90
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
???
temp
.
91
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
???
temp
.
92
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
???
temp
.
93
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
???
temp
.
94
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
???
temp
.
95
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
???
temp
.
96
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
97
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
98
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
99
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
100
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
101
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
102
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
103
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
104
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
105
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
106
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
107
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
108
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temp temp NULL

p
.
i
10
p2
.
???
30
temp
.
109
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temptemp NULL

p
.
i
10
p2
.
???
30
temp
.
110
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temptemp NULL

p
.
i
10
p2
.
???
30
temp
.
111
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temptemp NULL

p
.
i
10
p2
.
???
30
temp
.
112
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temptemp NULL

p
.
i
10
p2
.
???
30
temp
.
113
Introduction to Pointer
  • Now you should able to understand basic pointer
    operations below by using the simplified version
    of pointer representation technique explained in
    the previous slide int p, p2, temp int
    i 10 p ip2 new intp2 30temp
    pp p2p2 temptemp NULL

p
.
i
10
p2
.
???
30
temp
.
114
Conclusion
  • Each time we declare variable there are three
    main entities exist which are the variable name,
    its value, and address in memory referred by the
    variable
  • We can get the address of variables by using the
    syntax var_name
  • There is special variable which is called as
    pointer that can store/hold address referred by
    other variables
  • For variable of type pointer declare as T P, P
    can only store address referred by other
    variables with type T
  • Dereferencing is the term used to access the
    value of other variable point by the pointer
Write a Comment
User Comments (0)
About PowerShow.com