Pointer
Pointer
Pointers
1
Pointers
• The type of the variable that stores an address is
called a pointer
• long *pnumber;
• int *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
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
• double value[5];
18
Array and Pointers
• We can also use the name of an array as
though it was a pointer
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];
double *pvalue;