C++ Pointer
C++ Pointer
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
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;
8 4444
cout<<”Nilai pertama ialah ”
4444 5555
<<pertama<<endl;
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;
A Z
10
line
4 char* charPtr;
A Z
11
line
5 charPtr = &char1;
A Z 1293
12
line
6 temp = *charPtr;
A Z A 1293
1293 7757 2131 4455
13
line
7 *charPtr=char2;
Z Z A 1293
1293 7757 2131 4455
14
line
8 char2 = temp;
Z A A 1293
1293 7757 2131 4455
15
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
17
line
5 temp = p1;
c d p1 p2 temp
O H 941 6940 941
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
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.
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.
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
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);
33
Pointer to Structure
Structure can be accessed through pointers.
34
Pointer to Structure
35
Pointer to Structure
36
Selection Operator
Another operator that eliminates the problem
with pointers to structures – the selection
operator.
(* pointerName) . fieldName
SAME AS
38
Selection Operator
39