Professional Documents
Culture Documents
Pre-requisite
Data type
Variable
Array
Function call
Standard Input/Output
Outline
Computer Memory Structure
Addressing Concept
Introduction to Pointer
Pointer Manipulation
Summary
Computer Memory
Revisited
Computers store data in memory
slots
Each slot has an unique address
Variables store their values like
this:
Add
Conten Add Content Add Conten Add Conten
1000 i: 37
1001 j: 46
1002 k: 58
1003 m: 74
1004 a[0]: a
1005 a[1]: b
1006 a[2]: c
1007 a[3]:
\0
1008 ptr:
1001
1009
1010
1011
Computer Memory
Revisited
Add
r
Content
Add
r
Conten
t
Add
r
Conten
t
1000 i: 40
1001 j: 46
1002 k: 58
1003 m: 74
1004 a[0]: a
1005 a[1]: b
1006 a[2]: z
1007 a[3]:
\0
1008 ptr:
1001
1009
1010
1011
Addressing Concept
Pointer stores the address of
another entity
It refers to a memory location
int i = 5;
int *ptr;
ptr = &i;
Remember this?
scanf(%d, &i);
ptr = &i;
printf(i = %d\n, i);
Output:
i = 5
*ptr = 5
ptr = effff5e0
5
value of ptr =
address of i
in memory
Twin Operators
Add
r
Conten Add
t
r
Content
Add
r
Conten
t
Add
r
Conten
t
1000 i: 40
1001 j: 33
1002 k: 58
1003 m: 74
1004 ptr:
1001
1005
1006
1007
Twin Operators
*: De-reference operator
Add
r
Conten Add
t
r
Content
Add
r
Conten
t
Add
r
Conten
t
1000 i: 40
1001 j: 99
1002 k: 58
1003 m: 74
1004 ptr:
1001
1005
1006
1007
10
Example: Pass by
Reference
{
j = 5;
*ptr = 5;
void g()
void g()
int i = 3;
int i = 3;
f(i);
f(&i);
i = 3?
i = 5?
11
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
10
**pptr = 7;
Description
Value
*pptr = &i;
*ptr = -2;
12
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
10
*pptr = &i;
ptr
**pptr = 7;
Description
Value
*ptr = -2;
13
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
10
*pptr = &i;
ptr
*ptr = -2;
pptr
**pptr = 7;
Description
Value
14
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
10
*pptr = &i;
ptr
*ptr = -2;
pptr
int
**
*ptr
int
de-reference of ptr
**pptr = 7;
Description
Value
address of i
15
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; /* store address-of ptr to pptr */
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
10
*pptr = &i;
ptr
*ptr = -2;
pptr
**pptr = 7;
*pptr
Description
Value
address of i
address of
ptr
value of ptr
16
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
10
*pptr = &i;
ptr
*ptr = -2;
pptr
int
**
*ptr
int
de-reference of ptr
**pptr = 7;
Description
Value
address of i
address of
ptr
3
17
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
10
*pptr = &i;
ptr
*ptr = -2;
pptr
int
**
**pptr
int
**pptr = 7;
Description
Value
address of i
address of
ptr
7
18
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
10
*pptr = &i;
ptr
*ptr = -2;
pptr
int
**
*ptr
int
de-reference of ptr
**pptr = 7;
Description
Value
address of j
address of
ptr
10
19
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
*pptr = &i;
ptr
*ptr = -2;
pptr
int
**
**pptr
int
**pptr = 7;
Description
Value
address of j
address of
ptr
9
20
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
**pptr = 9;
int
integer variable
*pptr = &i;
ptr
*ptr = -2;
pptr
**pptr = 7;
*pptr
Description
Value
address of i
address of
ptr
value of ptr
21
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
Data Table
*ptr = 3;
Name
Type
ptr = &j;
int
integer variable
-2
**pptr = 9;
int
integer variable
*pptr = &i;
ptr
*ptr = -2;
pptr
int
**
*ptr
int
de-reference of ptr
**pptr = 7;
Description
Value
address of i
address of
ptr
-2
22
Pointer Arithmetic
Whats ptr + 1?
The next memory location!
Whats ptr - 1?
The previous memory location!
Whats ptr * 2 and ptr / 2?
Invalid operations!!!
23
Data Table
Name
Type
Description
a[0]
float
a[1]
float
a[2]
float
a[3]
float
ptr
float
*
*ptr
float
de-reference of float
pointer variable
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
24
Data Table
Name
Type
Description
a[0]
float
a[1]
float
a[2]
float
a[3]
float
ptr
float
*
address of
a[2]
*ptr
float
de-reference of float
pointer variable
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
25
Data Table
Name
Type
Description
a[0]
float
a[1]
float
a[2]
float
3.14
a[3]
float
ptr
float
*
address of
a[2]
*ptr
float
de-reference of float
pointer variable
3.14
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
26
Data Table
Name
Type
Description
a[0]
float
a[1]
float
a[2]
float
3.14
a[3]
float
ptr
float
*
address of
a[3]
*ptr
float
de-reference of float
pointer variable
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
27
Data Table
Name
Type
Description
a[0]
float
a[1]
float
a[2]
float
3.14
a[3]
float
9.0
ptr
float
*
address of
a[3]
*ptr
float
de-reference of float
pointer variable
9.0
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
28
Data Table
Name
Type
Description
a[0]
float
a[1]
float
a[2]
float
3.14
a[3]
float
9.0
ptr
float
*
address of
a[0]
*ptr
float
de-reference of float
pointer variable
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
29
Data Table
Name
Type
Description
a[0]
float
6.0
a[1]
float
a[2]
float
3.14
a[3]
float
9.0
ptr
float
*
address of
a[0]
*ptr
float
de-reference of float
pointer variable
6.0
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
30
Data Table
Name
Type
Description
a[0]
float
6.0
a[1]
float
a[2]
float
3.14
a[3]
float
9.0
ptr
float
*
address of
a[2]
*ptr
float
de-reference of float
pointer variable
3.14
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
31
Data Table
Name
Type
Description
a[0]
float
6.0
a[1]
float
a[2]
float
7.0
a[3]
float
9.0
ptr
float
*
address of
a[2]
*ptr
float
de-reference of float
pointer variable
7.0
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
32
ptr = &(a[2])
ptr = &(*(a + 2))
ptr = a + 2
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Type of a is float *
a[2] *(a + 2)
a is a memory address
constant
ptr is a pointer variable
33
Add
r
Add
r
Content
Add
r
Conten Add
t
r
Conten
t
1000 a[0]:
37.9
1001
1002
1003
1004 a[1]:
1.23
1005
1006
1007
34
Add
r
Add
r
Content
Add
r
Conten Add
t
r
Conten
t
1000 a[0]:
37.9
1001
1002
1003
1004 a[1]:
1.23
1005
1006
1007
35
Pros
Efficiency
Convenience
Cons
Error-prone
Difficult to debug
36
Summary
A pointer stores the address
(memory location) of another entity
Address-of operator (&) gets the
address of an entity
De-reference operator (*) makes a
reference to the referee of a pointer
Pointer and array
Pointer arithmetic
37