CPP Program On Pointer - WAY2IT

Latest Post :

Home Top Ad

CPP Program On Pointer



                              program  to demonstrate Pointer


#include<iostream.h>

#include<stdio.h>

#include<conio.h>



void main()

{

 clrscr();

 int n,*p;

 cout<<"\n Enter a number : ";

 cin>>n;

 p = &n;

 cout<<"\n Address of n : "<<p;

 cout<<"\n Value of n acessed through pointer: "<<*p;

 getch();

}

●▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
 
program to demonstrate array elements 
Accesed through pointer


#include<iostream.h>

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int a[10], i, *p;

cout<<" \nEnter ten numbers: ";

for(i=0;i<10;i++)

cin>>a[i];

p = a;

cout<<"\n \t Array elements accesed through pointer \n";

cout<<"\n -----------------------------------------\n";

for(i=0;i<10;i++)

{

cout<<"a["<<i<<"]\t";

}

for(i=0;i<10;i++)

{

cout<<" "<<*p<<"\t";

 p++;

}

getch();

}


●▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
 
program for function and pointers


#include<iostream.h>

#include<conio.h>

#include<stdio.h>

void sort(int*,int);

void main()

{

clrscr();

int a[10], i, *p;

cout<<" \nEnter ten numbers: ";

for(i=0;i<10;i++)

cin>>a[i];

p = a;

cout<<"\n \t Array elements before sorting \n";

cout<<"\n -----------------------------------------\n";

for(i=0;i<10;i++)

{

cout<<"a["<<i<<"]\t";

for(i=0;i<10;i++)

{

cout<<" "<<a[i]<<"\t";

 sort(p,10);

cout<<"\n \n \t \t Array elements after sorting \n";

cout<<"\n--------------------------------------\n";

for(i=0;i<10;i++)

{

cout<<"a["<<i<<"]\t";

}

}

for(i=0;i<10;i++)

{

cout<<" "<<p[i]<<"\t";

}

getch();

}

void sort(int* a,int n)

{

int i, j,t;

for(i=0;i<n-1;i++)

 {

  for(j=i+1;j<n;j++)

  {

   if(a[i]>a[j])

   {

    t = a[i];

    a[i] = a[j];

    a[j] = t;

   }

  }

 }

};


●▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
 
program for void pointer


#include<iostream.h>

#include<conio.h>

void main()

{

 clrscr();

 int n;

 char c;

 void *ptr;

 cout<<"\n Enter a number : ";

 cin>>n;

 cout<<"\n Enter a character: ";

 cin>>c;

 ptr = &n;

 cout<<"\n value of n acessed throughy void pointer: "<<*((int *)ptr);

 ptr = &c;

 cout<<"\n value of accessed through same void pointer:"<<*((char *)ptr);

 getch();


●▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
 
program for pointer to objects


#include<iostream.h>

#include<conio.h>

class person

{

 char name[20];

 int age;

 public:

 void get()

 {

  cout<<"Enter name and age: ";

  cin>>name>>age;

 }

 void show()

 {

  cout<<"\n Name:"<<name;

  cout<<"\n Age:  "<<age;

 }

};

void main()

{

 clrscr();

 person p;

 person *ptr;

 p.get();

   ptr = &p;

  ptr->show();

getch();

}


●▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
 
program for array –pointer –constant pointer


#include<iostream.h>

#include<conio.h>

main()

{

 clrscr();

 int intarray[5] = {31,54,77,52,93};

 int *ptrint;

 ptrint = intarray;

 for(int j=0;j<5;j++)

 //cout<<intarray[j]<<endl;//arrays

 //cout<<*(intarray +j)<<endl;  //pointers

 cout<<*(ptrint++)<<endl;

 getch();

}


●▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
 
program to demonstrate nesting of classes 
 strings & pointers


#include<iostream.h>

#include<conio.h>

#include<stdio.h>



class colony

{

 char *address;

 public:

     void get()

     {

      cout<<"\n Enter address: ";

      gets(address);

     }

     void show()

     {

      cout<<"\n,"<<address;

     }

};



class room

{

 int roomno,bldgno;

 public:



     colony c1;

     void get()

     {

      cout<<"\n Enter room no.: ";

      cin>>roomno;

      cout<<"\n Enter building no.";

      cin>>bldgno;

      c1.get();

     }



     void show()

     {

     cout<<"\n Adresss\n";

     cout<<"\n--------------";

     cout<<"\n"<<roomno<<"/"<<bldgno;

     c1.show();

  }

};



void main()

{

clrscr();

room r;

r.get();

r.show();

getch();

}