Data Structures and Algorithms: Introduction To Pointers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Data Structures and Algorithms

Lecture 3:
Introduction to Pointers

Tauseef Iftikhar

Department of Computer Science


Government College University, Lahore.
Today’s Agenda

Pointers Basics
Introduction

Pointers and Arrays


Today’s Agenda

Pointers Basics
Introduction

Pointers and Arrays


Pointer variable
Pointer is a variable (pointer variable) which holds the memory
addresses as their value.
I Usually a variable contains some specific value, we say
variable directly references a value
I a pointer variable contains the address of a variable which
hold some specific value, we say pointer indirectly references a
value, called indirection.

..
1021 .
1017 45 variable
1013
1009 1017 pointer variable
1005
..
1001 .
Table: memory layout
Pointer declaration and initialization
Pointers, like any other variables, must be declared before they can
be used. The declaration
1 i n t ∗ count ;
We read it from right to left, count is pointer to integer. Yes count
is now a pointer to an integer type variable/memory location.
Similarly,
1 c h a r ∗ c P t r ; \\ c P t r i s a p o i n t e r t o a c h a r a c t e r
value
2 f l o a t ∗ f P t r ; \\ f P t r i s a p o i n t e r t o f l o a t v l a u e
3 i n t ∗ i P t r , c o u n t ; \\What i s t h i s ?
4 int ∗ iPtr , ∗ jPtr ;
5 i n t ∗ i P t r = NULL ; \\ NULL p o i n t e r
Address of operator (&) is used to initialize a pointer variable.
Address of operator (&) returns the address of already existing
variable.
1 i P t r = & count ;
Dereferencing operator (*)

The * operator commonly referred to as the dereferencing


operator, returns value for the variable to which its operand points.
For example
1 i n t count = 5;
2 i n t ∗ i P t r = & count ;
3 cout << ∗ i P t r <<e n d l ;
4 cout << c o u n t <<e n d l ;
5 ∗ iPtr = 10;
6 cout << c o u n t <<e n d l ;
7 c i n >> ∗ i P t r ;
Don’t Confuse

Pointer declaration(int *) and Dereferencing operator(*)


Reference operator (&)and Address of operator(&)
1 i n t count = 5;
2 i n t & temp = c o u n t ;
3 cout << temp <<e n d l ;
4 cout << c o u n t <<e n d l ;
5 temp = 1 0 ;
6 cout << c o u n t <<e n d l ;
7 cout << &count <<e n d l ;
8 cout << &temp<<e n d l ;
9 i n t ∗ i P t r = & count ;
10 cout << ∗ i P t r <<e n d l ;
Pointer to pointer
As name indicates it will be a pointer to a pointer to some object.
1 i n t count = 10;
2 i n t ∗p = & c o u n t ;
3 i n t ∗q = & p ;

..
1021 .
1017 45 variable
1013
1009 1017 pointer variable
1005
1001 1009 pointer to pointer
Table: pointer to pointer

1 cout << c o u n t << ∗p << ∗∗ q <<e n d l ;


2 cout << p << q <<e n d l ;
3 cout << &p << ∗q << &q <<e n d l ;
Calling functions by Reference
There are 3 ways in C++ to pass arguments to a function
I Call-by-value
I Call-by-reference with reference arguments
I Call-by reference with pointer arguments
We have discussed call-by-value and call-by-reference with
reference arguments in Lecture 2. Just for recall
1
2 i n t cubeByValue ( i n t ) ;
3 v o i d c u b e B y R e f e r e n c e ( i n t &) ;
4 v o i d c u b e B y R e f e r e n c e P o i n t e r ( i n t ∗) ;
5
6 i n t main ( v o i d ) {
7 i n t number = 2 ;
8
9 c o u t<<”The o r i g i n a l v a l u e o f number i s ”<< number<<e n d l ;
10 i n t c u b e = c u b e B y V a l u e ( number ) ;
11 c o u t<< ”The v l a u e o f number a f t e r c a l l B y V a l u e ( ) i s ”<<number <<e n d l ;
12 c u b e B y R e f e r e n c e ( number ) ;
13 c o u t<< ”The v a l u e o f number a f t e r c a l l B y R e f e r e n c e ( ) i s ” << number<<e n d l ;
14 c u b e B y R e f e r e n c e P o i n t e r (&number ) ;
15 c o u t<<”The v a l u e o f number a f t e r c a l l B y R e f e r e n c e P o i n t e r i s ”<< number<<e n d l ;
16 return 0;
17 }
Constant pointers

The const qualifier enables the programmer to inform the compiler


that value of a particular variable should not be modified.
The const qualifier can be used to enforce the principle of least
privilege.
Always award a function enough access to the data in its
parameters to accomplish its specified task, but no more.
For example, size of array is passed in a function as parameter, its
value should not be changed in a function accidentally so it should
be treated as const
Pointer Arithmetic

Pointers are valid operands in arithmetic expressions, assignment


expression and comparison expression. Valid operators are:
I ++, –
I integer addition (+,+=), subtraction (-, -=): When an
integer is added to or subtracted from a pointer, the pointer
incremented by that integer times the size of the object to
which the pointer refers.
When an integer is added or subtracted from a pointer, the pointer
is incremented or decremented by that integer times the size of the
object pointed to.
Secret behind arrays
Array name is a constant pointer to first element.

1017 56 a[4]
1013 89 a[3]
1009 56 a[2]
1005 12 a[1]
1001 34 a[0]
..
.
0100 1001 a
Table: 1D-Array

1 cout << &a [ 0 ] <<e n d l ;


2 cout << a <<e n d l ;
3 cout << a [ 0 ] <<e n d l ;
4 cout << ∗ a <<e n d l ;
5
Pointer and Array
A pointer can be used to access an array.
1 i n t a [ 5 ] = {20 ,30 ,40 ,50 ,60};
2 i n t ∗p = a ;
3 cout << p [ 2 ] <<e n d l ;
4 p = &a [ 2 ] ;
5 cout << p [ 2 ] <<e n d l ;
6 cout << p [ −2] <<e n d l ;
7

Some conclusions
I address calculations *(a+index): we know the pointer
arithmetic. What about *a+ index ?
I *(a+index) is equivalent to a[index]: [] is fancy dereferencing
operator
I array name is constant pointer it can’t be allocated to point
to some other location.

You might also like