1
Pointers in C
Call By Reference

2
Pre-requisite
Basics of the C programming language
Data type
Variable
Array
Function call
Standard Input/Output
e.g.
printf(), scanf()

3
Outline
Computer Memory Structure
Addressing Concept
Introduction to Pointer
Pointer Manipulation
Summary

4
Computer Memory Revisited
Computers store data in memory slots
Each slot has an
unique address
Variables store their values like this:
Addr
Content
Addr
Content
Addr
Content
Addr
Content
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

5
Computer Memory Revisited
Altering the value of a variable is indeed
changing the content of the memory
e.g.
i = 40; a[2] = ‘z’;
Addr
Content
Addr
Content
Addr
Content
Addr
Content
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

6
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 */

7

8
What actually ptris?

9
Twin Operators
&: Address-of operator
Get the
address
of an entity
e.g.
ptr = &j;
Addr
Content
Addr
Content
Addr
Content
Addr
Content
1000
i: 40
1001
j: 33
1002
k: 58
1003
m: 74
1004
ptr: 1001
1005
1006
1007

10
Twin Operators
*: De-reference operator
Refer to the
content
of the referee
e.g.
*ptr = 99;
Addr
Content
Addr
Content
Addr
Content
Addr
Content
1000
i: 40
1001
j: 99
1002
k: 58
1003
m: 74
1004
ptr: 1001
1005
1006
1007
