0% found this document useful (0 votes)
13 views26 pages

Pointer

Uploaded by

Kashpia Hamid
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)
13 views26 pages

Pointer

Uploaded by

Kashpia Hamid
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/ 26

CE 204: Computer Programming Sessional

Pointers
1
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


2
well as by the variable name
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


3
pointer 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
4
meaning by the context
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

5
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;
6
}
Address of Operator

7
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


8
pointer
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; //rn is a synonym for n

rn is declared to be a reference to the variable


n, which must already have been declared 9
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;
}
10
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
11
variable, references are not variables
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<<", &rn 3=" <<&rn3
<< endl;
} 12
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

13
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
14
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
15
**ppn = 44
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]


16
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; 17
Array and Pointers
cout<<"\n value[3]="<<&value[3];
cout<<"\n pvalue+4 ="<<pvalue+4;
cout<<"\n value[4]="<<&value[4];
cout<<endl;
}

18
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
19
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; 20
Array and Pointers
for(int i=0; i<m; i++)
{for(int j=0; j<n; j++)cin>>value[i][j];}
for(int i=0; i<m; i++)
{for(int 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;
}

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

int salary;

string address;

double ce206[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 22
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
23
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; 24


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

You might also like