Pointers in C
Pre-requisite
Basics of the C programming
language
Data type
Variable
Array
Function call
Standard Input/Output
e.g. printf(), scanf()
2
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
Altering the value of a variable is
indeed changing the content of the
memory
Add
r
e.g. i = 40; a[2] = z;
Conten Add
t
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;
/* declare a pointer variable */
ptr = &i;
/* store address-of i to ptr */
printf(*ptr = %d\n, *ptr);
/* refer to referee of ptr */
Why do we need Pointer?
Simply because its there!
It is used in some circumstances in
C
Remember this?
scanf(%d, &i);
What actually ptr is?
ptr is a variable storing an
address
ptr is NOT storing the actual value
ptr
address of i
int i = 5;
of
i
int *ptr;
ptr = &i;
printf(i = %d\n, i);
Output:
printf(*ptr = %d\n, *ptr);
i = 5
printf(ptr = %p\n, ptr);
*ptr = 5
ptr = effff5e0
5
value of ptr =
address of i
in memory
8
Twin Operators
&: Address-of operator
Get the address of an entity
Add
r
e.g. ptr = &j;
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
Refer to the content of the referee
Add
r
e.g. *ptr = 99;
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
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
a[3]
float
float array element
(variable)
ptr
float
*
float pointer variable
*ptr
float
de-reference of float
pointer variable
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Value
?
12
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
a[3]
float
float array element
(variable)
ptr
float
*
float pointer variable
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
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
3.14
a[3]
float
float array element
(variable)
ptr
float
*
float pointer variable
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
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
3.14
a[3]
float
float array element
(variable)
ptr
float
*
float pointer variable
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
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
3.14
a[3]
float
float array element
(variable)
9.0
ptr
float
*
float pointer variable
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
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
3.14
a[3]
float
float array element
(variable)
9.0
ptr
float
*
float pointer variable
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
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
6.0
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
3.14
a[3]
float
float array element
(variable)
9.0
ptr
float
*
float pointer variable
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
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
6.0
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
3.14
a[3]
float
float array element
(variable)
9.0
ptr
float
*
float pointer variable
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
Pointer Arithmetic and
Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
Data Table
Name
Type
Description
a[0]
float
float array element
(variable)
6.0
a[1]
float
float array element
(variable)
a[2]
float
float array element
(variable)
7.0
a[3]
float
float array element
(variable)
9.0
ptr
float
*
float pointer variable
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
Pointer Arithmetic and
Array
float a[4];
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)
a is a memory address constant
ptr is a pointer variable
A+I gives address of ith element in
array
*(A+I) is the element stored in ith
location of array
21
Pointers and Functions
22
Example 1: Pass by value
and Reference
Modify behaviour in argument
passing
void f(int j)
void f(int *ptr)
{
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
int *sum(int , int );
Void main(){
Int a,b,*s;
Printf(enter two numbers);
Scanf(%d %d,&a,&b);
Printf(a=%d b=%d,a,b);
S=sum(a,b);
Printf(s=%d,*s);}
25
int *sum(int a, int b)
{
output
int s;
Enter two numbers
s=a+b;
4 5
return(&s);
a=4 b=5 s=9
}
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;
/* declare a pointer-to-integer variable */
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
int * integer pointer variable
*ptr = -2;
31
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
/* declare a pointer-to-pointer-to-integer variable */
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
int * integer pointer variable
int
**
integer pointer pointer
variable
Double
32
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
/* store address-of i to ptr */
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
**
integer pointer pointer
variable
*ptr
int
de-reference of ptr
**pptr = 7;
Description
int * integer pointer variable
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
int * integer pointer variable
int
**
integer pointer pointer
variable
int * de-reference of pptr
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
**
integer pointer pointer
variable
*ptr
int
de-reference of ptr
**pptr = 7;
Description
int * integer pointer variable
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
**
integer pointer pointer
variable
**pptr
int
de-reference of dereference of pptr
**pptr = 7;
Description
int * integer pointer variable
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
**
integer pointer pointer
variable
*ptr
int
de-reference of ptr
**pptr = 7;
Description
int * integer pointer variable
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
**
integer pointer pointer
variable
**pptr
int
de-reference of dereference of pptr
**pptr = 7;
Description
int * integer pointer variable
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
int * integer pointer variable
int
**
integer pointer pointer
variable
int * de-reference of pptr
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
**
integer pointer pointer
variable
*ptr
int
de-reference of ptr
**pptr = 7;
Description
int * integer pointer variable
Value
address of i
address of
ptr
-2
40
Advice and Precaution
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