Académique Documents
Professionnel Documents
Culture Documents
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.
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.
Example :
1. char a=A; char *p; p = &a; OR char *p = &a;
2. double b=2.5;
double *q;
q = &b;
OR
double *q=&b;
5
7 9640
9640 9642
Example
#include<iostream> void main() { int pertama=8; int *pAlamat; pAlamat = &pertama; cout<<Nilai pertama ialah <<pertama<<endl; cout<<Nilai *pAlamat ialah <<*pAlamat<<endl;
pertama pAlamat
8
4444 5555
pertama
pAlamat
8
4444
4444
5555
Output: Nilai pertama ialah 8 Nilai *pAlamat ialah 8 Kandungan pAlamat ialah 4444 Alamat untuk pAlamat ialah 5555
#include<iostream.h> Example int main() { int pertama=7; int *pAlamat; pAlamat = &pertama; cout<<Nilai pertama ialah <<pertama<<endl;
pertama
pAlamat
7
6666 8888
pertama
pAlamat
7
6666
6666
8888
pertama
pAlamat
52
6666
6666
8888
Output: Nilai pertama ialah 7 Nilai *pAlamat ialah 7 Nilai *pAlamat ialah 52 Nilai pertama ialah 52
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() { char char1 = A; char char2 = Z; char temp; char* charPtr; charPtr = &char1; temp = *charPtr; *charPtr = char2; char2 = temp; }
1 2 3 4 5 6 7 8
char1
char2 Z
temp
A 1293
7757
2131
10
char1 A 1293
char2
Z 7757
temp
charPtr
2131
4455
11
char1 A 1293
char2 Z 7757
temp
charPtr 1293
2131
4455
12
char1
char2 Z 7757
temp
A 2131
charPtr
A
1293
1293
4455
13
line 7 *charPtr=char2;
char1
char2 Z 7757
temp A 2131
charPtr
Z
1293
1293 4455
14
char1 Z 1293
char2 A 7757
temp A 2131
charPtr
1293 4455
15
Example
What is the output for this example?
Line #include<iostream.h> main() { char c = O,d = H; char *p1, *p2, *temp; p1 = &c; p2 = &d; temp = p1; p1 = p2; p2 = temp; cout<<*p1<<*p2; }
16
1 2 3 4 5 6 7 8 9
line 1 2 3 4
char char p1 = p2 =
c
O 941
d H 6940
p1
941 2131
p2 6940 4455
temp
17
line 5
temp = p1;
c O 941
d H 6940
p1 941 2131
p2
6940 4455
temp
941
18
line 6
p1 = p2;
c O 941
d H 6940
p1 6940 2131
p2 6940 4455
temp
941
19
line 7
p2 = temp;
temp
c O 941 d H 6940 p1 6940 2131 p2 941 4455
941
line 8 output :
HO
21
nombor
//Example: #include<iostream.h> void main() { int nombor = 6, jumlah; int *pnum=&nombor; jumlah = *pnum * *pnum; cout<<Hasil darab *pnum dengan <<*pnum ialah <<jumlah<<endl;
pnum
}
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 **q
23
a
integer variable
p
58 4444 4444 5555
Pointer to pointer to integer
q
5555 6666
Pointer to integer
#include<iostream.h> main() { int a; int *p; int **q; a = 58; p = &a; q = &p; cout<< a <<endl; cout<< *p <<endl; cout<< **q <<endl; }
Output:
58 58 58
24
25
Example
void main() { //Pass by reference int a = 5; int b = 7; exchange (a,b); } void exchange(int& x, int& y) a b { 7 5 int temp = x; x=y; x y y= temp; 5 return; temp }//exchange
26
Example
void main() { a b //Passing pointers 7 5 int a = 5; y int b = 7; exchange (&a,&b); } px py void exchange(int* px, int* py) { 5 int temp = *px; temp *px=*py; *py= temp; return; 27 }//exchange
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
*( 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);
30
Example of program that use a pointer to refer the elements in an array. #include <iostream.h> main() { const int saiz = 5; int i; int gred[saiz]={98,88,90,65,83}; int *pNilai = gred; //*pNilai=&gred[0] for( i = 0; i<saiz; i++) cout<<Unsur <<i << tatasusunan ini ialah <<*( pNilai + i )<<endl; }
#include <iostream.h> main() { const int saiz = 5; int i; int gred[saiz]={98,88,90,65,83}; for( i = 0; i<saiz; i++) cout<<Unsur <<i << tatasusunan ini ialah <<gred[i]<<endl; }
Output: Unsur 0 tatasusunan ini ialah 98 Unsur 1 tatasusunan ini ialah 88 Unsur 2 tatasusunan ini ialah 90 Unsur 3 tatasusunan ini ialah 65 Unsur 4 tatasusunan ini ialah 83
32
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.
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
38
Selection Operator
39