Monday, September 22, 2014

Pointers in C++

Pointer To Integers & Pointer To Pointer

**************************************************************************************
#include<iostream.h>
void main()
{
int var1=11;
int *ptr;
// pointer to integer
// dealing with pointer *ptr and integer vriable var1
ptr=&var1;
// Errors
//      *ptr=&var1;
//       ptr=var1;
//       var1=&ptr;
cout<<"Adress that pointer ptr is holding=                                      "<<ptr;
cout<<endl<<"Value of integer variable var=                                   "<<var1;
cout<<endl<<"Adress of var1 vriable using adress operator=              "<<&var1;
cout<<endl<<"Value of that variable for those this pointer is assign=  "<<*ptr;
cout<<endl<<"Own Address of pointer ptr=                                      "<<&ptr;

//dealing with pointer to pointer
int **ptr1;

//   Errors
// ptr1=var1;
// ptr1=ptr;
// ptr1=&var1;
// ptr1=&*ptr;
ptr1=&ptr;
cout<<endl<<"Adress that pointer** ptr1 is holding=            "<<ptr1;
cout<<endl<<"Adress that pointer* ptr is holding=               "<<*ptr1;
cout<<endl<<"Own adress of pointer** ptr1=                       "<<&ptr1;
cout<<endl<<"Adress that pointer* ptr is holding=                "<<&ptr;
cout<<endl<<"Value of variable that adress is saved in ptr*= "<<**ptr1;

//  Derefrencing addresses
var1=3;
*ptr=4;        // use all these three one by one and see the change in output
**ptr1=5;
 cout<<endl<<"\n\t var1=3 *ptr=4 **ptr1=5\n";
cout<<endl<<"Adress that pointer ptr is holding=                             "<<ptr;
cout<<endl<<"Value of integer variable var=                                  "<<var1;
cout<<endl<<"Adress of var1 vriable using adress operator=              "<<&var1;
cout<<endl<<"Value of that variable for those this pointer is assign= "<<*ptr;
cout<<endl<<"Own Address of pointer ptr=                                   "<<&ptr;
cout<<endl<<"Adress that pointer** ptr1 is holding=                      "<<ptr1;
cout<<endl<<"Adress that pointer* ptr is holding=                         "<<*ptr1;
cout<<endl<<"Own adress of pointer** ptr1=                                "<<&ptr1;
cout<<endl<<"Adress that pointer* ptr is holding=                         "<<&ptr;
cout<<endl<<"Value of variable that adress is saved in ptr*=          "<<**ptr1;

}

Share this

0 Comment to "Pointers in C++"