Chapter 4 Pointer
Chapter 4 Pointer
Constant Pointer
Exa: &Umur
Pointer
Variable Pointer
Exa : P1= &Umur;
CONSTANT POINTER
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
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.
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;
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
int *P1 ;
P1=new int ;
*P1=25;
The new operator creates just enough memory to hold an integer value pointer
by P1.
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.
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 :
//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]; }