Vous êtes sur la page 1sur 39

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.

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.

Pointer Declaration and Definition


Syntax : type * identifier;

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

The type of pointer indicates 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

7 9640

9640 9642

the variable p points to x


6

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

cout<<Kandungan pAlamat ialah\n <<pAlamat<<endl;


cout<<Alamat untuk pAlamat ialah\n <<&pAlamat<<endl; }

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

cout<<Nilai *pAlamat ialah <<*pAlamat<<endl;


*pAlamat = 52;

pertama

pAlamat

52
6666

6666
8888

cout<<Nilai *pAlamat ialah <<*pAlamat<<endl;


cout<<Nilai pertama ialah <<pertama<<endl;

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

line 1 2 3 char char1 = A; char char2 = Z; char temp;

char1

char2 Z

temp

A 1293

7757

2131

10

line 4 char* charPtr;

char1 A 1293

char2
Z 7757

temp

charPtr

2131

4455

11

line 5 charPtr = &char1;

char1 A 1293

char2 Z 7757

temp

charPtr 1293

2131

4455

12

line 6 temp = *charPtr;

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

line 8 char2 = temp;

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,d = H; *p1, *p2, *temp; &c; &d;

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 :

cout<< *p1 << *p2;

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
//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

dereferenced once dereferenced twice

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

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) 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

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 arrays 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 array and index

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

Relationship between a Pointer and an Array


From the example, each element in array gred can be reach using pointer :
Array gred[0] gred[1] gred[2] gred[3] gred[4] Pointer *pNilai *( pNilai + 1 ) *( pNilai + 2 ) *( pNilai + 3 ) *( pNilai + 4 ) Value in Array 98 88 90 65 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

Vous aimerez peut-être aussi