GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Sunday, December 19, 2010

C++ Pointer Variable

Here's a small demo to know about pointer:

#include <iostream>
#include <iomanip>

int main(){

    int a = 45;
    int* ptr = &a;

   cout << "An initial value for a: 45" << endl;
   cout << setw(6) << "a: " << a << endl;
   cout << setw(6) << "&a: " << &a << endl;
   cout << setw(6) << "ptr: " << ptr << endl;
   cout << setw(6) << "*ptr: " << *ptr << endl;
   cout << setw(6) << "&ptr: " << &ptr << endl;

   cout << endl;

   cout << "After giving a value to *ptr: 100 " << endl;
   cout << setw(6) << "a: " << a << endl;
   cout << setw(6) << "&a: " << &a << endl;
   cout << setw(6) << "ptr: " << ptr << endl;
   cout << setw(6) << "*ptr: " << *ptr << endl;
   cout << setw(6) << "&ptr: " << &ptr << endl;

   return 0; 

}

The result for above code is:
An initial value of a: 45
   a: 45
  &a: 0x22ff3c
 ptr: 0x22ff3c 
*ptr: 45
&ptr: 0x22ff38

Give new value to *ptr: 100
   a: 100
  &a: 0x22ff3c
 ptr: 0x22ff3c 
*ptr: 100
&ptr: 0x22ff38

Share/Bookmark

No comments:

Post a Comment