Pointers

Address de-reference operator(&)

When we declare a variable, it must be stored in a concrete location in a succession of cells(the computer memory). We generally do not decide where the variable is to be placed. Fortunately, that is done automatically by the compiler and the operating system on runtime. But once the operating system has assigned an address to a variable we may sometimes be interested in knowing where it has been stored.

This can be done by preceding the variable identifier by the ampersand sign(&). which literally means "address of " For example: - ptr = $a;

would assign the address of variable a to variable ptr. The ampersand refers to the address of a variable in memory and not its contents.

If variable a has been placed in the memory address 1776 then value of ptr will be 1776.

Pointer is a variable which holds the address of another variable in the memory.

Reference Operator (*)

Using a pointer we can access directly the value stored in the variable pointed to by it. This can be done by preceding the pointer identifier with the reference operator asterisk (*), that can be literally translated as " value pointed by ". If we write:

a=25;

b=a;

b= *ptr;

it can be read as "b equal to value pointed by ptr". The variable b would take the value 25, since ptr has the value 1776 and has the value pointed to by 1776 is 25.

You must understand that the variable ptr stores 1776, but *ptr refers to the value stored in the address 1776, that is 25.

Use of Pointers

  1. Pointers can be used for swapping variables without changing their physical address in memory.

  2. A pointer helps in traversing an array.

  3. Pointers are used to pass arguments to functions by reference. The arguments may be arrays or strings.

  4. Pointers are used to obtain memory from system using the dynamic memory allocation operator new.

  5. Pointers can return memory to the system using dynamic memeory allocation operaotr delete.

  6. Pointers are used for the creation of data structures like linked lists.

Declaring Variables of Type Pointer

type *pointer-name;

for example:

int *number;

char *character;

float *greatnumber;

Pointer Initialization

When declaring pointers we may want to specify explixitly which variable we want them to point to.

The statemets

int number;

int *t=&number;

are equivalent to:

int number;

int *t;

t=&number;

When a pointer assignation takes place we assign the address of the variable it points to, and not the value pointed to. At the moment of declaring a pointer, the asterisk(*) indicates only that it is a pointer. It does not indicate the reference operator asterisk (*). Remember, they are two different operaotrs, although they are written with the same sign.

Consider the following segment:

(where we suppose that the value of p is 15 / and p is located at 2514 of memory address and q is located at 1236 of the memory address.)

..............

..............

int *q; //pointer declaration

int p=15;

q=&p; //pointer initialization

cout<<"\n Value of p= "<<p;

cout<<"\n Address of p= "<<&p;

cout<<"\n Value of p= "<<q;

cout<<"\n Address of p= "<<q;

cout<<"\n Value of q= "<<&q;

...

...

The output would be:

Value of p = 15

Address of p = 2514

Value of p = 15

Address of p = 2514

Address of q = 1236