0% found this document useful (0 votes)
12 views

L8 - Pointers

Uploaded by

alamin shawon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

L8 - Pointers

Uploaded by

alamin shawon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CE 204: Computer Programming Sessional

Pointers
1
Tazwar Bakhtiyar Zahid

Lecturer,
Department of Civil Engineering,
BUET

2
Pointers
• The type of the variable that stores an address is
called a pointer
• Pointer variables have the derived type “pointer to
T”, where T is the type of the object to which the
pointer points
• A memory bin has an address and is usually
expressed in hexadecimal
• We can store the address of a memory bin (i.e. of a
variable name) in a pointer
• Then we can access that bin by the pointer as well as
by the variable name 3
Declaring Pointers

• long* pnumber; //declares pnumber of type pointer


to long

• long *pnumber;

• int *pa, *pb;


• double *pmark;

• Note: The asterisk may be attached to either type


name or variable name.

• int* pnumber, number; //declare pnumber as pointer


4
to int and number as int
Indirection Operator
• When we put * immediately before a pointer, (in
statements other then declaration) it refers to the
contents of the memory bin pointed to by the
pointer variable

• Thus if px is a pointer then *px (in statements other


then declaration) means the value stored in the
address referred to by px

• The symbol * is used as multiplication operator, as


indirection operator and also to declare a pointer,
the compiler is able to distinguish the meaning by
the context 5
Address of Operator
• The address-of operator ‘&’ is a unary operator that
obtains the address of a variable in the memory

• int *pnumber;

• pnumber = &number; //&number means the


address of number and it is stored in pnumber

6
Address of Operator
#include <iostream>
using namespace std;
int main()
{
int x, y;
int *px;
int *py;
x=10;
px=&x;
py=&y;
y=*px;
cout<<" x ="<<x<<" y ="<<y<<endl;
cout<<"\n *px ="<<*px<<" *py ="<<*py<<endl;
} 7
Address of Operator

8
Initializing Pointers
• It is easy to initialize a pointer with the address of a
variable

• However, the variable must have to be declared


prior to the pointer declaration

int age = 0; //initialized integer variable

int* page = &age; //initialized pointer

• We can also initialize a pointer as below

int* pnum=0; //pointer not pointing to anything

• A pointer initialized in this way is called a null


pointer 9
References
• A reference is an alias or synonym for another
variable, it is declared by the syntax

type& ref-name = var-name;

• where type is the variable’s type, ref-name is the


name of the reference, and var-name is the name of
the variable

• For example, in the declaration

int& rn=n; //r is a synonym for n

rn is declared to be a reference to the variable n,


which must already have been declared 10
Using References
#include <iostream>
using namespace std;
int main()
{
int n=44;
int& rn=n; // rn is a synonym for n
cout << "n = " << n << ", rn = " << rn << endl;
--n;
cout << "n = " << n << ", rn = " << rn << endl;
rn *= 2;
cout << "n = " << n << ", rn = " << rn << endl;
}
11
Using References
Output: n = 44, rn = 44
n = 43, rn = 43
n = 86, rn = 86
• The two identifiers n and rn are different names for the
same variable; they always have the same value
• Like constants, references must be initialized when
they are declared
• But unlike a constant, a reference must be initialized to
a variable, not a literal
• Although a reference must be initialized to a variable,
references are not variables
12
References Are Not Separate Variables
#include <iostream>
using namespace std;
int main()
{
int n=44;
int& rn=n; // rn is a synonym for n
cout <<"&n = " << &n << ", &rn = " << &rn
<< endl;
int& rn2=n; // rn2 is another synonym for n
int& rn3=rn; // rn3 is another synonym for n
cout <<"&rn2="<<&rn2<<",&rn3=" <<&rn3
<< endl;
} 13
References Are Not Separate Variables

Output:
&n = 0064fde4, &rn = 0064fde4
&rn2 = 0064fde4, &rn3 = 0064fde4
• N.B.: This output varies from program to program
• The first line of output shows that n and rn have the
same address: 0064fde4
• Thus they are merely different names for the same
object

14
Pointers to Pointers
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n=44;
cout << " n = " << n << endl;
cout << " &n = " << &n << endl;
int* pn=&n; // pn holds the address of n
cout << " pn = " << pn << endl;
cout << " &pn = " << &pn << endl;
cout << " *pn = " << *pn << endl;
int** ppn=&pn; // ppn holds the address of pn
15
Pointers to Pointers
cout << " ppn = " << ppn << endl;
cout << " &ppn = " << &ppn << endl;
cout << " *ppn = " << *ppn << endl;
cout << "**ppn = " << **ppn << endl;
}
Output: n = 44
&n = 0064fd78
pn = 0064fd78
&pn = 0064fd7c
*pn = 44
ppn = 0064fd7c
&ppn = 0064fd80
*ppn = 0064fd78
**ppn = 44 16
Array and Pointers
• The array and a pointer have similarity in that both
contain an address

• Most significant difference between a pointer and


an array is that you can modify the address stored
in a pointer, while the address that an array refers
to is fixed

• double value[5];

• double* pvalue = value; // stores the address of the


array values in pvalues

• Now pvalue+1 will mean the address of value[1]


17
Array and Pointers
#include <iostream>
using namespace std;
int main()
{
double value[5], *pvalue;
pvalue = value;
cout<<"\n pvalue = "<<pvalue;
cout<<"\n value[0]="<<&value[0];
cout<<"\n pvalue+1 ="<<pvalue+1;
cout<<"\n value[1]="<<&value[1];
cout<<"\n pvalue+2 ="<<pvalue+2;
cout<<"\n value[2]="<<&value[2];
cout<<"\n pvalue+3 ="<<pvalue+3; 18
Array and Pointers
cout<<"\n value[3]="<<&value[3];
cout<<"\n pvalue+4 ="<<pvalue+4;
cout<<"\n value[4]="<<&value[4];
cout<<endl;
}

19
Array and Pointers
• We can also use the name of an array as though it
was a pointer

• We can address the elements of an array such as:


*(value+i) refers to value[i] of the array

• For a two dimensional array value[ ][ ],


*(*(value+i)+j) refers to the element value[i][j];

• *(value+i) refers to the address of the first element


of row i

• *(value+i)+j refers to the address of offset j at row i

20
Array and Pointers
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double value[10][10];
int m,n;
cout<<" No. of rows (max 10)? ";
cin>>m;
cout<<" No. of columns (max 10)? ";
cin>>n;
cout<<" Input the elements of the matrix
(row wise)\n ";
int i,j; 21
Array and Pointers
for(i=0; i<m; i++)
{for(j=0; j<n; j++)cin>>value[i][j];}
for(i=0; i<m; i++)
{for(j=0; j<n; j++)cout<<setw(7)<<value[i][j];
cout<<endl;}
cout<<"value[1][2] = "<<value[1][2]<<endl;
cout<<"*(*(value+1)+2)="<<*(*(value+1)+2)
<<endl;
}

22
Dynamic Memory Allocation
• When we declare a variable or an array in the source
code in the form

int salary;

string address;

double ce204[50];

the corresponding memory requirement is decided at


compile time

• All of this amount of memory will be allocated, at


execution of the program, whether we need them or
not 23
Dynamic Memory Allocation
• Such a situation can be avoided by using dynamic
memory allocation

• Dynamic memory allocation means that the amount


of memory to be allocated will be decided at run time

• By definition, dynamically allocated variables cannot


be declared at compile time and so they cannot be
named in the source code

• When we run programs, there may be unused


memory in the computer

24
Dynamic Memory Allocation
• This unused memory is called free store or heap

• We can allocate space within this free store for a new


variable by using a special c++ operator new

• Also we can de-allocate a previously allocated


memory by the operator delete

• Memory once allocated by the new operator won’t be


available for other variables unless it is de-allocated
by delete operator

double *pvalue;

pvalue = new double; 25


Dynamic Memory Allocation
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n, i;
cout<<" How many numbers? ";
cin>>n;
double *number= new double[n];
double sum=0.0;
cout<<" Input the numbers\n ";
for(i=0; i<n; i++){cin>>*(number+i);}
for(i=0; i<n; i++){sum += *(number+i);}
cout<<"Average = "<<sum/n<<endl;
delete [] number;
} 26
Thank You!
27

You might also like