Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere

Pointers in C++

A pointer is a variable that holds a memory address,A pointer is a variable whose value is the address of another variable.
Pointers are variables which points to a locations in the computer's memory which can be accessed by their identifier, by using pointers the program does not need to care about the physical address of the data in memory,it simply uses the identifier whenever it needs to refer to the variable.
The memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address. memory cells are ordered can be easily located in the memory by means of its unique address.
For example, the memory cell with the address 1001 always follows immediately after the cell with address 1000 and precedes the one with 1002.
Lets take a real world example,Each house in a socierty has a no which identifies it's location in society.

type *var-name;
Here, type is a valid C++ type and var-name is the name of the pointer variable. The asterisk used to declare a pointer is the same asterisk that you use for multiplication. Here in this statement the asterisk is being used to designate a variable as a pointer.

int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

Two special pointer operators are: * and &.
The & is unary operator that returns the memory address of its operand. It is “the address of” operand.
The * is complement of &.It is also a unary operator and returns the value located at the address that follows.

A pointers can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*). The operator itself can be read as "value pointed to by".

Sample program-
using namespace std;
int main ()
    int num = 100; // actual variable declaration.
    int *pointer; // pointer variable

    pointer = # // store address of var in pointer variable

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

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

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

    return 0;
When the above code is compiled and executed, it produces result something as follows:
Value of var variable: 100
Address stored in pointer variable: 0xbfc601xc
Value of *pointer variable: 20

Pointer arithmetics
Pointer arithmetic operations are bit different than regular integer types.
One can do only addition and subtraction operations on poietrs, and it works according to the size of the data type to which they point.
For example: char always has a size of 1 byte, short is generally larger than that, and int and long are even larger; the exact size of these being dependent on the system. For example, let's imagine that in a given system, char takes 1 byte, short takes 2 bytes, and long takes 4.