Tuesday, 4 December 2012

Pointers in C++

   Pointers in C++ are similar to pointers in C. They are used to hold address of other variables. Thus pointer is a way to access the data in an indirect manner. For example consider the program fragment given below.

                            int data=10;  // data is an integer variable.
                            int *ptr;      // ptr is a pointer which can hold the address of an integer variable
                            p=&a;     // & is used to get the address of variable data.
Now we can access the content of data using either data itself or the pointer variable ptr.

                            cout<<*ptr;  // will print 10.
                            cout<<data; // will print 10.

The content of variable data is accessed by using *ptr, which gives the content of the address in ptr.

Click here to download C++ Programs illustrating the working of pointers.

No comments:

Post a Comment