POINTER
LEARNING OUTCOMES (LO)
4.2 Show program using the concept of pointer
4.2.1 Define pointer.
4.2.2 Identify the syntax to declare a pointer.
4.2.3 Assign the address of variable to
pointer.
4.2.4 Manipulate the value of variables using
pointer.
4.2.5 Apply new and delete operators.
4.2.6 Write, run, test and debug program using pointer.
4.3 Identify the relationship between pointer and array
4.3.1 Identify the relationship between pointer and
array.
4.3.2 Apply pointer to manipulate the elements of array.
4.3.3 Write, run, test and debug program using pointer and
array.
What is Pointer ?
What is Pointer ?
What is Pointer ?
Pointersare variables whose values are memory
addresses.
Constant Pointer
Exa: &Umur
Pointer
Variable Pointer
Exa : P1= &Umur;
CONSTANT POINTER
By preceding a variable identifier with an ampersand,
you are representing the memory address of the
variable. & Operator is refer as Reference
Operator/ Address Operator.
Example:
int Umur ; //Define Variable
&Umur = Constant Pointer = the memory address of the variable’ Umur’.
This kind of pointer (&Umur) are Constant Pointer, because the address to
which the pointer point can NEVER be changed by program.
& Operator is refer as Reference Operator/ Address Operator.
VARIABLE POINTER
A variable pointer is a pointer whereby the address to
which it is points can be changed.
Example :
int *P1; //Declare a Pointer
int Umur =20 ; //Define and initialize variable
int Tahun ;
P1=&Umur ;
//Direct a pointer to point at Umur, the value inside P1=Memory Address of Umur.
cout<<P1; //Output = 7834A84D990F ( memory address in hex).
cout<<*P1; //the content of memory pointed to by PI, output =20.
2 Bytes (16 bits)
20
P1 Umur
We can then alter P1 to point at any address in memory because they are
Variable pointer.
P1=&umur;
p1 umur
However, we cannot alter &Umur because it is Constant pointer.
A pointer can only point to a memory address, thus :
P1 =&umur; //Correct
P1=umur; //WRONG
DEFINE POINTER.
Syntax :
<datatype being pointed to > * <pointer identifier>;
Example :
int *Penunjuk ; //Integer Pointer
char *Penunjuk2; //Character Pointer
float *Penunjuk3; //Floating Point Pointer
INITIALIZE VARIABLE
POINTER
There are 2 way to Initialize a pointer to point to a value : Static Pointer
and Dynamic Pointer.
Constant Pointer Static Pointer
Exp: &Umur P1=&Umur;
Pointer
Variable Pointer
Dynamic Pointer
Exp : int *P1;
P1= new int;
a) Static Pointer
Static Pointer : Allocating memory Statically by defining a variable and the direct
the pointer to point to it.
int *P1 ;
int Umur ;
P1=&Umur ;
*P1= 25; // Statically initialize the pointer P1
//The star / Asterisk (*) in front of a pointer can be read as “the contents of “.
//Assign the contents of P1 to 25;
* Operator, commonly referred to as the Indirection Operator or
Dereferencing Operator
& Operator is referred as Reference Operator/ Address Operator.
2 Bytes ( can store -32768 to 32767)
25
p1
int : Umur
We say that this type of initialization is static because the allocation of memory
used to store the pointer value is fixed when we define the variable (Umur). The
compiler already set aside enough memory to store the value (int = 2 bytes) and
the memory REMAINS already reserved for the variable and cannot be used for
anything else.
NORZIMAH BT CHE HASSAN
DYNAMIC POINTER
Allocating memory dynamically (using new operator) and initializing the
pointer to point to a value.
DYNAMIC POINTER
By allocating memory dynamically, we meant to set aside memory when it is
needed.
int *P1 ;
P1=new int ;
*P1=25;
The new operator creates just enough memory to hold an integer value pointer
by P1.
When it is not needed anymore, we can deallocate it by using the delete
operator.
delete P1;
This operation will free up the memory pointed to by P1 and use for other use
by the system.
The delete operator does NOT delete the pointer, it simply destroys the data to
which the pointer points.
The new operator is used to dynamically allocate memory for pointer data,
it creates just enough memory to hold value pointed by pointer.
The delete operator is used to deallocate memory pointed to by a pointer,
delete the operator simply mean destroy the data.
RELATIONSHIP BETWEEN POINTER
AND ARRAY.
Arrays and pointers are intimately related in C/C++ and often may be used
interchangeably.
An array name can be thought of as a constant pointer
Example 2 : Apply pointer to point at Array
int *P1; //Declare Pointer
int Umur[3] //Declare Array
P1=Umur ; //Direct a Variable pointer (P1) to point at the First element of array
Umur.
// the value inside PI=Memory Address of
Umur[0].
Umur[0] Umur[1] Umur[2]
Again, we can alter P1 to point at any address in memory because they are Variable
pointer.
However, we cannot alter &Umur because it is Constant pointer.
A pointer can only point to a memory address, thus :
P1 = &Umur; //Correct
P1=Umur; //WRONG
Provided ‘Umur’ is a variable.
If ’Umur’ is an Array then :
P1 = &Umur; //WRONG
P1=Umur; //Correct
Because Umur is a constant Pointer in this case.
ACCESSING POINTER
DATA
Example :
Method 1:
char sptr[4]=”HAL”;
char *sptr;
sptr = &sptr;
Method 2 :
char sptr[4]=”HAL”;
char *sptr = &sptr;
ACCESSING POINTER
DATA
ACCESSING POINTER
DATA
// Array
cout<<sptr[0]; //output = H.
cout<<sptr[1]; //outpur =A.
cout<<sptr; //Output :”HAL”;
// Pointer
cout<<*sptr; //output = H,
cout<<*(sptr+1); //output =A.
cout<<*&(sptr+1)[0]; //Output=A.
cout<<*&(sptr+1)[1]; //Output=L.
APPLY POINTER TO MANIPULATE
THE ELEMENTS OF ARRAY.
Example :
//Define integer Pointer
int *Pnj;
//Define Array with initialized value
int Nombor[3]={1,2,3};
//Point Pointer to Array Nombor
Pnj = Nombor;
//Manipulate
cout<<*(Pnj+1) * *(Pnj+2);// Content of (Pnj+1) * Contents of (Pnj+2)
cout<<*Pnj * *(pnj+1);
APPLY POINTER TO MANIPULATE
THE ELEMENTS OF ARRAY.
Pnj 1 2 3
*Pnj *(Pnj+1) *(Pnj+2)
Pop quiz!!!!
What would be printed by the following statements?
double *pt;
double a[3]={1.2, 2.3, 3.4};
pt=&a[1];
pt+=1;
cout<<*pt<<endl;
A. 1.2
B. 2.3
C. 3.3
D. 3.4
E. None of the above.
int main() {
{ int *Array[3] = {&n1,&n2,&n3};
int DisplayArray(int,int,int);
int n1, n2, n3; for (int i = 0; i < 3; i++)
cout<<"Enter value inside an Array[0]= "; {
cin>> n1; cout<< "Array[" << i << "]=
cout<<"Enter value inside an Array[1]= "; "<<*Array[i]<<"\n";
cin>> n2; }
cout<<"Enter value inside an Array[2]= "; return 0;
cin>> n3; }
DisplayArray(n1,n2,n3);
return 0;
system("PAUSE");
}
#include <iostream> // output the returned value
using namespace std; cout << "Average value is: " << avg <<
endl;
// function declaration
double getAverage(int arr[], int size); return 0;
system("PAUSE");
int main () }
{
// an int array with 5 elements. double getAverage(int arr[], int size)
int balance[5] = {1000, 2, 3, 17, 50}; {
double avg; int i, sum = 0;
double avg;
// pass pointer to the array as an
argument. for (i = 0; i < size; ++i)
avg = getAverage( balance, 5 ) ; { sum += arr[i]; }
avg = double(sum) / size;
return avg;
}