BITG 1113:
Pointer
LECTURE 12
1
Objectives:
• To understand the concept of pointer.
• Declaration and Assignment of a Pointer.
• Arithmetic Operation using Pointer.
• Pointer to pointer.
• Pointers and functions.
• Relationship between a Pointer and an
Array.
• Pointer to structure.
2
Introduction
A pointer is a derived type. Its value may be
any of the addresses available in the computer for
storing and accessing data.
In a computer, every process is stored in a
compartment or small area inside the memory.
These memories have their own addresses.
By using this address, the computer can
identify where a data or an instruction is stored
physically in a computer.
3
Introduction
Variable declaration :
int nilai;
Only shows the value that is stored inside
nilai, without knowing where it is stored.
The name of the variable is an ‘@’ for the
address where it is stored.
Each time, the variable being used, the
computer will translate the name of that variable to
its address.
By using pointer, the programmer can
determine the address for any location where a
value is stored. 4
Pointer Declaration and Definition
Syntax :
type * identifier;
Example :
1. char a=‘A’;
char *p; OR
p = &a; char *p = &a;
2. double b=2.5;
double *q; OR
q = &b; double *q=&b;
The type of pointer indicates 5
the type of value it refers.
Pointer Declaration and definition
int x=7;
int *p;
p = &x;
Suppose that the address of the int variable x is 9640
x 7 p 9640 the variable p
points to x
9640 9642
6
Example
pertama pAlamat
#include<iostream>
void main() 8
{
4444 5555
int pertama=8;
int *pAlamat;
pAlamat
pAlamat = &pertama; pertama
8 4444
cout<<”Nilai pertama ialah ”
4444 5555
<<pertama<<endl;
cout<<”Nilai *pAlamat ialah ”
Output:
<<*pAlamat<<endl; Nilai pertama ialah 8
Nilai *pAlamat ialah 8
cout<<”Kandungan pAlamat ialah\n ” Kandungan pAlamat ialah
<<pAlamat<<endl; 4444
Alamat untuk pAlamat ialah
5555
cout<<”Alamat untuk pAlamat ialah\n”
<<&pAlamat<<endl;
Example
#include<iostream.h>
int main() pertama pAlamat
{
int pertama=7; 7
int *pAlamat; 6666 8888
pAlamat = &pertama;
pertama pAlamat
cout<<”Nilai pertama ialah ”
<<pertama<<endl; 7 6666
6666 8888
cout<<”Nilai *pAlamat ialah ”
<<*pAlamat<<endl; pertama pAlamat
*pAlamat = 52; 52 6666
6666 8888
cout<<”Nilai *pAlamat ialah ” Output:
<<*pAlamat<<endl; Nilai pertama ialah 7
Nilai *pAlamat ialah 7
Nilai *pAlamat ialah 52
cout<<”Nilai pertama ialah ” Nilai pertama ialah 52
<<pertama<<endl;
Example
The following program slice swaps the
contents of the variables char1 and char2 but
uses the address and dereferencing operators to
do so.
Line #include<iostream.h>
main()
{
1 char char1 = ‘A’;
2 char char2 = ‘Z’;
3 char temp;
4 char* charPtr;
5 charPtr = &char1;
6 temp = *charPtr;
7 *charPtr = char2;
8 char2 = temp; 9
}
line
1 char char1 = ‘A’;
2 char char2 = ‘Z’;
3 char temp;
char1 char2 temp
A Z
1293 7757 2131
10
line
4 char* charPtr;
char1 char2 temp charPtr
A Z
1293 7757 2131 4455
11
line
5 charPtr = &char1;
char1 char2 temp charPtr
A Z 1293
1293 7757 2131 4455
12
line
6 temp = *charPtr;
char1 char2 temp charPtr
A Z A 1293
1293 7757 2131 4455
13
line
7 *charPtr=char2;
char1 char2 temp charPtr
Z Z A 1293
1293 7757 2131 4455
14
line
8 char2 = temp;
char1 char2 temp charPtr
Z A A 1293
1293 7757 2131 4455
15
Example
What is the output for this example?
Line #include<iostream.h>
main()
{
1 char c = ‘O’,d = ‘H’;
2 char *p1, *p2, *temp;
3 p1 = &c;
4 p2 = &d;
5 temp = p1;
6 p1 = p2;
7 p2 = temp;
8 cout<<*p1<<*p2;
9 }
16
line
1 char c = ‘O’,d = ‘H’;
2 char *p1, *p2, *temp;
3 p1 = &c;
4 p2 = &d;
c d p1 p2 temp
O H 941 6940
941 6940 2131 4455
17
line
5 temp = p1;
c d p1 p2 temp
O H 941 6940 941
941 6940 2131 4455
18
line
6 p1 = p2;
c d p1 p2 temp
O H 6940 6940 941
941 6940 2131 4455
19
line
7 p2 = temp;
temp
c d p1 p2
O H 6940 941 941
941 6940 2131 4455
line
8 cout<< *p1 << *p2;
output : HO
Arithmetic Operation Using Pointer
Arithmetic operation can also be done using
pointer.
A pointer will take the value stored at the
address that is kept by the pointer and
compute them just like operations on
normal variables such as add, multiply,
divide, minus and unary operator.
21
nombor pnum
//Example:
#include<iostream.h> 6
void main()
{
int nombor = 6, jumlah;
int *pnum=&nombor;
jumlah = *pnum * *pnum;
cout<<”Hasil darab *pnum dengan “
<<”*pnum ialah “<<jumlah<<endl;
}
Output:
Hasil darab *pnum dengan *pnum ialah 36 22
Pointer to pointer
So far the pointers we have been using have
pointed directly to data. It is possible and often
with advanced data structures necessary to use
pointers that point to other pointers.
eg: *p dereferenced once
**q dereferenced twice
23
a p q
integer 58 4444 5555
variable
4444 5555 6666
Pointer to Pointer to
integer pointer to
#include<iostream.h> integer
main() {
int a;
int *p;
int **q;
a = 58;
p = &a; Output:
q = &p;
cout<< a <<endl; 58 58 58
cout<< *p <<endl;
cout<< **q <<endl; 24
}
Pointers and functions
• C++ provides two ways to pass parameters to
functions : pass by value and pass by reference.
• When we passed by reference, C++ passes the
address of the parameter variable, and the
parameter name becames an alias for the variable.
• We can add an alternative to pass by reference:
pass a pointer and use it to change the original
variable.
25
Example
void main()
{
//Pass by reference
int a = 5;
int b = 7;
exchange (a,b);
}
void exchange(int& x, int& y)
{
int temp = x;
x=y;
y= temp; a b
return;
7 5
}//exchange
x y
5
temp 26
Example
void main()
{
//Passing pointers a b
int a = 5;
int b = 7;
7 5
exchange (&a,&b);
y
}
void exchange(int* px, int* py)
{
int temp = *px; px py
*px=*py;
*py= temp;
5
return;
}//exchange temp
27
Relationship between a
Pointer and an Array
A pointer and an array have a close
relationship.
In C++ Programming Language, the name of
an array without the index is the first address
of that array. In other words, the name of an
array is actually a pointer to that array.
So, the name of an array can be used to be
set as an initial value for a pointer.
The name of an array will refer to the
address of the first element of that array.
28
Relationship between a
Pointer and an Array
The difference between a pointer and an array’s
name is, a pointer points to any address in the
memory but the name of an array points only to
the first element of that array and it is fixed.
Example :
double value[10];
double *pValue = value;
From the declaration above, *pValue will store
the first element of array value which is the address
of value[0].
29
Relationship between a
Pointer and an Array
To achieve the next element in an array,
use this formula :-
*( penuding + i )
where , i represents the location of the element
in an array.
Example :
double value[5]={2.5,4.1,5.6,4.7,4.3};
double *pValue = value;
cout<<*(pValue + 2);
- will print the third element of the array :
5.6 30
Example of program that use an Example of program that use a
array and index pointer to refer the elements in
an array.
#include <iostream.h> #include <iostream.h>
main() main()
{ {
const int saiz = 5; const int saiz = 5;
int i; int i;
int gred[saiz]={98,88,90,65,83}; int gred[saiz]={98,88,90,65,83};
for( i = 0; i<saiz; i++) int *pNilai = gred;
cout<<”Unsur “<<i //*pNilai=&gred[0]
<<” tatasusunan ini ialah “
<<gred[i]<<endl; for( i = 0; i<saiz; i++)
} cout<<”Unsur “<<i
Output:
<<” tatasusunan ini ialah “
Unsur 0 tatasusunan ini ialah 98
Unsur 1 tatasusunan ini ialah 88 <<*( pNilai + i )<<endl;
Unsur 2 tatasusunan ini ialah 90 }
Unsur 3 tatasusunan ini ialah 65
Unsur 4 tatasusunan ini ialah 83
Relationship between a
Pointer and an Array
From the example, each element in array
gred can be reach using pointer :
Array Pointer Value in Array
gred[0] *pNilai 98
gred[1] *( pNilai + 1 ) 88
gred[2] *( pNilai + 2 ) 90
gred[3] *( pNilai + 3 ) 65
gred[4] *( pNilai + 4 ) 83
32
Relationship between a
Pointer and an Array
Example :
double value[5]={2.5,4.1,5.6,4.7,4.3};
double *pValue = &value[1];
cout<<*(pValue + 2);
- will print the fourth element of the array :
4.7
33
Pointer to Structure
Structure can be accessed through pointers.
Example : use SAMPLE structure with pointers.
The first thing is define a pointer for the
structure as shown below.
Refer to whole structure
34
Pointer to Structure
The parentheses are absolutely necessary –
because the precedence priority of the
member operator is higher than the priority of the
indirection operator.
If the parentheses are not used, C++ applies
the dot operator first and the asterisk operator
next.
In other words, *ptr.x is interpreted as
*(ptr.x)
35
Pointer to Structure
36
Selection Operator
Another operator that eliminates the problem
with pointers to structures – the selection
operator.
The selection operator is at the same level in the
Precedence Table as the member operator.
The token for the selection operator is an arrow
formed by the minus sign and the greater than
symbol (->).
The token is placed immediately after the pointer
identifiers and before the member to be
referenced.
37
Selection Operator
(* pointerName) . fieldName
SAME AS
pointerName -> fieldName
38
Selection Operator
39