Contents
1. Program to explain working of Simple Pointer Variable
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
clrscr();
int a =5;
int * p;
p = &a;
cout << "\n a = " << a;
cout << "\n &a = " << &a;
cout << "\n p = " << p;
cout << "\n *p = " << *p;
cout << "\n &p = " << &p;
getch();
}
output:
a = 5
&a = 0x7fdca105fc
p = 0x7fdca105fc
*p = 5
&p = 0x7fdca105f0
2. Program to explain that a Pointer Variable can point different variable at different
point of time
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a = 10, b = 20, c = 30;
int * p;
p = &a;
cout << "\n *p = " << *p;
p = &b;
cout << "\n *p = " << *p;
p = &c;
cout << "\n *p = " << *p;
getch();
}
Output:
*p = 10
*p = 20
*p = 30
3. Program to explain that Multiple Pointers can point a Single Variable
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a = 10;
int *p1, *p2, *p3;
p1 = &a;
p2 = &a;
p3 = &a;
cout << "\n *p1 = " << *p1;
cout << "\n *p2 = " << *p2;
cout << "\n *p3 = " << *p3;
*p2 = 50;
cout << "\n *p1 = " << *p1;
cout << "\n *p2 = " << *p2;
cout << "\n *p3 = " << *p3;
*p3 = *p1 + *p2;
cout << "\n *p1 = " << *p1;
cout << "\n *p2 = " << *p2;
cout << "\n *p3 = " << *p3;
getch();
}
Output:
*p1 = 10
*p2 = 10
*p3 = 10
*p1 = 50
*p2 = 50
*p3 = 50
*p1 = 100
*p2 = 100
*p3 = 100
4. Program to use Pointer Variable in Simple Volume of Cylinder example
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
float r, h, v;
float *pr, *ph, *pv;
const float PI = 3.14;
pr = &r;
ph = &h;
pv = &v;
cout << " Enter Radius & Height : ";
cin >> *pr >> *ph;
*pv = PI * (*pr) * (*pr) * (*ph);
cout << "\n Volume of Cylinder = " << *pv;
getch();
}
Output:
Enter Radius & Height : 10 10
Volume of Cylinder = 3140
5. Program to explain Array of Pointers
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a = 10, b = 20, c = 30;
int *p[3], *t;
int i;
p[0] = &a;
p[1] = &b;
p[2] = &c;
cout << " Values are : ";
for( i=0 ; i<3 ; i++ )
{
cout << " " << *p[ i ];
}
t = p[0];
p[0] = p[2];
p[2] = t;
cout << "\n Values are : ";
for( i=0 ; i<3 ; i++ )
{
cout << " " << *p[ i ];
}
getch();
}
Output:
Values are : 10 20 30
Values are : 30 20 10
6. Program to explain Array and Pointer
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a[ ] = { 10, 20, 30, 40, 50 };
int *p;
int i;
p = &a[0]; /* p = a; */
cout << "\n Array are (a[ i ]): ";
for( i=0 ; i<5 ; i++ )
{
cout << " " << a[ i ];
}
cout << "\n Array are (i[a]): ";
for( i=0 ; i<5 ; i++ )
{
cout << " " << i[a];
}
cout << "\n Array are (*(a+i)): ";
for( i=0 ; i<5 ; i++ )
{
cout << " " << *(a+i);
}
cout << "\n Array are (*(p+i)): ";
for( i=0 ; i<5 ; i++ )
{
cout << " " << *(p+i);
}
cout << "\n Array are (p[ i ]): ";
for( i=0 ; i<5 ; i++ )
{
cout << " " << p[ i ];
}
cout << "\n Array are (*p++): ";
for( i=0 ; i<5 ; i++ )
{
cout << " " << *p++;
}
getch();
}
Output:
Array are (a[ i ]): 10 20 30 40 50
Array are (i[ a ]): 10 20 30 40 50
Array are (*( a+i )): 10 20 30 40 50
Array are (*( p+i )): 10 20 30 40 50
Array are (p[ i ]): 10 20 30 40 50
Array are ( *p++ ): 10 20 30 40 50
7. PROGRAM TO POINTER WITH ARRAYS
#include<conio.h>
#include<iostream.h>
void main()
{
int numbers[50],*ptr;
int n,i;
clrscr();
cout<<"How Many Elements are needed?\n";
cin>>n;
cout<<"Enter the numbers one by one\n";
for(i=0;i<n;i++)
cin>>numbers[i];
ptr=numbers;
int sum=0;
for(i=0;i<n;i++)
{
if(*ptr%2==0)
{
sum=sum+*ptr;
}
ptr++;
}
cout<<"Sum of even numbers : "<<sum;
getch();
}
Output:
How Many Elements are needed?
5
Enter the numbers one by one
10
15
20
25
30
Sum of even numbers : 60
8. Program to manipulation of pointers
#include<conio.h>
#include<iostream.h>
void main()
{
int a=10,*ptr;
ptr=&a;
clrscr();
cout<<"The Value of a is : "<<a<<endl;
*ptr=(*ptr)/2;
cout<<"The Value of a is : "<<(*ptr);
getch();
}
Output:
The Value of a is : 10
The Value of a is : 5
9. Program to pass Array to Function using Pass by Pointer
#include <iostream.h>
#include <conio.h>
void display( int* );
void main ()
{
clrscr();
int a[ ] = { 10, 20, 30, 40, 50 };
display(a);
getch();
}
void display( int *p )
{
int i;
cout << "\n Array are : ";
for( i=0 ; i<5 ; i++ )
{
cout << " " << *(p+i);
}
}
Output:
Array are : 10 20 30 40 50
10. Program to explain Passing by Pointer
#include <iostream.h>
#include <conio.h>
void square( int* );
void main()
{
clrscr();
int a = 10;
square(&a);
cout << "\n a = " << a;
getch();
}
void square( int *px )
{
*px = (*px) * (*px);
cout << "\n *px = " << *px;
}
Output:
*px = 100
a = 100
11. Program to explain Returning by Pointer
#include <iostream.h>
#include <conio.h>
int* max( int*, int* );
void main()
{
clrscr();
int a = 10, b = 20, *p;
p = max(&a, &b);
cout << "\n Maximum = " << *p;
getch();
}
int* max( int *pa, int *pb )
{
if( *pa > *pb )
return pa;
else
return pb;
}
Output:
Maximum = 20
12. Program to explain Pointer with 2-D Array ( Matrix )
#include <iostream.h>
#include <conio.h>
void main ()
{
clrscr();
int a[2][3] = { { 10, 20, 30 },
{ 40, 50, 60 } };
int *p;
int i, j;
cout << "\n\n Matrix-A : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << " " << *(*(a+i)+j);
}
cout << "\n";
}
cout << "\n\n Matrix-A : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << " " << *(a[ i ]+j);
}
cout << "\n";
}
p = &a[0][0];
cout << "\n\n Matrix-A : \n";
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << " " << *(p+3*i+j);
}
cout << "\n";
}
getch();
}
Output:
Matrix-A :
10 20 30
40 50 60
Matrix-A :
10 20 30
40 50 60
Matrix-A :
10 20 30
40 50 60
13. Program to explain Pointer to Function
#include <iostream.h>
#include <conio.h>
void Add( int, int );
void Sub( int, int );
void main ()
{
int a = 10, b = 20;
void (*pf)(int, int);
clrscr();
pf = Add;
(*pf)(a, b); /* Add(a, b); */
pf = Sub;
(*pf)(a, b); /* Sub(a, b); */
getch();
}
void Add( int x, int y)
{
int s;
s = x + y;
cout << "\n Sum = " << s;
}
void Sub( int x, int y)
{
int d;
d = x - y;
cout << "\n Difference = " << d;
}
Output:
Sum = 30
Difference = -10
14. Program to Pass String by Pointer
#include <iostream.h>
#include <conio.h>
void display(char*);
void main()
{
clrscr();
char s1[25] = "Easy";
display(s1);
getch();
}
void display( char *ps )
{
while( *ps ) // while(*ps != '\0')
{
cout << *ps;
ps++;
}
}
Output:
Easy
15. Program to explain Array of Pointers to Strings
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char *ps[3] = { "Easy", "Programming", "24" };
for( i=0 ; i<3 ; i++ )
{
cout << " " << ps[ i ];
}
getch();
}
Output:
Easy Programming 24
16. Program to explain Dynamic MemoryManagement
using new and delete Operator
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int *p;
p = new int;
*p = 10;
cout << "\n Value : " << *p;
delete p;
getch();
}
Output:
Value : 10
16(i). Program to explain Dynamic MemoryManagement
using new and delete Operator
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int *p;
p = new int(10);
cout << "\n Value : " << *p;
delete p;
getch();
}
Output:
Value : 10
16(ii). Program to explain Dynamic MemoryManagement using new and delete Operator with Array
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int *p;
int i;
p = new int[5];
for( i=0 ; i<5 ; i++ )
{
cout << "Enter Value : ";
cin >> *(p+i);
}
cout << "\n Values are : ";
for( i=0 ; i<5 ; i++ )
{
cout << " " << *(p+i);
}
delete[ ] p;
getch();
}
Output:
Enter Value : 10
Enter Value : 20
Enter Value : 30
Enter Value : 40
Enter Value : 50
Values are : 10 20 30 40 50
17. Program to explain Pointer to Object
#include <iostream.h>
#include <conio.h>
class Distance
{
private:
int feet;
float inches;
public:
void getDist()
{
cout << "Enter Feet : ";
cin >> feet;
cout << "Enter Inches : ";
cin >> inches;
}
void showDist()
{
cout << "\n" << feet << "\'-" << inches << "\"";
}
};
void main()
{
clrscr();
Distance *p;
p = new Distance;
p->getDist();
p->showDist();
getch();
}
Output:
Enter Feet : 10
Enter Inches : 5.5
10'-5.5"
0 Comments