What is a pointer ?

A pointer is a special type of variable.It keeps the address of another variable as its value. Pointer is a powerful feature of C++ which differentiates it from other languages like Java.In C++ pointers are used to access the memory and manipulate address.

Following is the general way of defining a pointer in C++

type *variable_name

e.g:- int *var

C++ example for pointer

#include <iostream>

using namespace std;

int main () {
   int  var = 100;   // variable declaration.
   int  *ptr;        // pointer variable declaration

   ptr = &var;       // store address of var in pointer variable

   cout << "Value of var variable: ";
   cout << var << endl;

   // print the address stored in the pointer variable
   cout << "Address stored in ptr variable: ";
   cout << ptr << endl;

   // access the value at the address available in pointer
   cout << "Value of *ptr variable: ";
   cout << *ptr << endl;

   return 0;
}

 

 

Advertisements