Professional Documents
Culture Documents
Itp 6
Itp 6
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
8
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
Pointer Arithmetic
Whats ptr ++?
Ptr is incremented by size of its data type
Whats ptr --?
Ptr is decremented by size of its data type
Whats ptr + 2 and ptr 2?
The content of ptr will get incremented by
product of integer value and size of int
type.
11
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
?
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
float *ptr;
ptr = &(a[2]);
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)
22
{
j = 5;
*ptr = 5;
void main()
void main()
int i = 3;
int i = 3;
f(i);
f(&i);
i = 3?
i = 5?
23
Example 2: Returning a
pointer from a function
Syntax
Data type
*function_name(arguments);
int *sum(int , int );
24
25
26
Example 2:Pointer to
functions
data type (*variable name)
(argument types);
int (*fnp) (int,int);
int sum(int,int);
fnp=sum;(fnp now points to
function sum)
27
int sum(int,int);
Main(){
int a,b,s,(*fnp)(int,int);
Printf(enter two nos.);
Scanf(%d%d,&a,&b);
Printf(a=%d b=%d,a,b) ;
fnp=sum;
28
S= (*fnp) (a,b);
Printf(sum=%d,s);}
int sum(int a, int b)
{
output
int s;
Enter two numbers
s=a+b;
4 6
return s;
a=4 b=6 sum=10
}
29
An Illustration(pointers to
pointers)
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;
30
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;
31
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
32
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
33
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
34
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
35
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
36
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
37
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
38
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
39
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
40
Pros
Efficiency
Convenience
Cons
Error-prone
Difficult to debug
41
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
42