Data Structures and Algorithms: Introduction To Pointers
Data Structures and Algorithms: Introduction To Pointers
Data Structures and Algorithms: Introduction To Pointers
Lecture 3:
Introduction to Pointers
Tauseef Iftikhar
Pointers Basics
Introduction
Pointers Basics
Introduction
..
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 (*)
..
1021 .
1017 45 variable
1013
1009 1017 pointer variable
1005
1001 1009 pointer to pointer
Table: pointer to pointer
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
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.