Professional Documents
Culture Documents
Pre-requisite
Basics of the C programming language
Data type Variable Array Function call
Outline
Computer Memory Structure
Addressing Concept
Introduction to Pointer Pointer Manipulation Summary
1008
4
1010
1011
1008
5
1010
1011
Addressing Concept
Pointer stores the address of another entity
int i = 5; int *ptr; ptr = &i; printf(*ptr = %d\n, *ptr); /* declare a pointer variable */ /* store address-of i to ptr */ /* refer to referee of ptr */
int i = 5; int *ptr; ptr = &i; printf(i = %d\n, i); printf(*ptr = %d\n, *ptr); printf(ptr = %p\n, ptr);
ptr
i
Output:
i = 5 *ptr = 5 ptr = effff5e0
address of i
5
value of ptr = address of i in memory
Twin Operators
&: Address-of operator
Get the address of an entity e.g. ptr = &j;
Twin Operators
*: De-reference operator
Refer to the content of the referee e.g. *ptr = 99;
10
void f(int j) {
j = 5;
} void g() { int i = 3; f(i); } }
*ptr = 5;
void g() { int i = 3; f(&i);
i=3 ?
i=5 ?
11
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
12
Data Table Name i j Type int int Description integer variable integer variable Value 5 10
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
13
Data Table Name i j ptr Type int int int * Description integer variable integer variable integer pointer variable Value 5 10
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
14
Data Table Name i j ptr Type int int int * Description integer variable integer variable integer pointer variable Value 5 10
pptr
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
15
Data Table Name i j ptr Type int int int * int Description integer variable integer variable integer pointer variable de-reference of ptr Value 5 10 address of i 5
pptr
*ptr
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; /* store address-of ptr to pptr */ *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
16
Data Table Name i j ptr Type int int int * int * Description integer variable integer variable integer pointer variable de-reference of pptr Value 5 10 address of i
pptr
*pptr
address of ptr
value of ptr (address of i)
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
17
Data Table Name i j ptr Type int int int * int Description integer variable integer variable integer pointer variable de-reference of ptr Value 3 10 address of i
pptr
*ptr
address of ptr
3
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
18
Data Table Name i j ptr Type int int int * int Description integer variable integer variable integer pointer variable de-reference of de-reference of pptr Value 7 10 address of i
pptr
**pptr
address of ptr
7
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
19
Data Table Name i j ptr Type int int int * int Description integer variable integer variable integer pointer variable de-reference of ptr Value 7 10 address of j
pptr
*ptr
address of ptr
10
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
20
Data Table Name i j ptr Type int int int * int Description integer variable integer variable integer pointer variable de-reference of de-reference of pptr Value 7 9 address of j
pptr
**pptr
address of ptr
9
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
21
Data Table Name i j ptr Type int int int * int * Description integer variable integer variable integer pointer variable de-reference of pptr Value 7 9 address of i
pptr
*pptr
address of ptr
value of ptr (address of i)
An Illustration
int i = 5, j = 10;
int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2;
22
Data Table Name i j ptr Type int int int * int Description integer variable integer variable integer pointer variable de-reference of ptr Value -2 9 address of i
pptr
*ptr
address of ptr
-2
Pointer Arithmetic
Whats ptr + 1?
23
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value ? ? ? ? ?
24
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value ? ? ? ? address of a[2] ?
25
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value ? ? 3.14 ? address of a[2] 3.14
26
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value ? ? 3.14 ? address of a[3] ?
27
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value ? ? 3.14 9.0 address of a[3] 9.0
28
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value ? ? 3.14 9.0 address of a[0] ?
29
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value 6.0 ? 3.14 9.0 address of a[0] 6.0
30
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value 6.0 ? 3.14 9.0 address of a[2] 3.14
31
Data Table Name a[0] a[1] a[2] a[3] ptr *ptr Type float float float float float Description float array element (variable) float array element (variable) float array element (variable) float array element (variable) de-reference of float pointer variable Value 6.0 ? 7.0 9.0 address of a[2] 7.0
32
ptr++;
*ptr = 9.0; ptr = ptr - 3; *ptr = 6.0;
ptr += 2;
*ptr = 7.0;
33
Addr Content
Addr Content
Addr Content
Addr Content
1000
1004 1008
34
a[0]: 37.9
a[1]: 1.23 a[2]: 3.14
1001
1005 1009
1002
1006 1010
1003
1007 1011
Addr Content
Addr Content
Addr Content
Addr Content
1000
1004 1008
35
a[0]: 37.9
a[1]: 1.23 a[2]: 3.14
1001
1005 1009
1002
1006 1010
1003
1007 1011
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