Introduction To Pointers:

Examples: Please look at V\common\user\gmanwani\Spring2000\c202\classwork

Definition of a Pointer:
Pointers are a type of variable that allow you to specify
the address of a variable. They provide a convenient means of passing arguments to functions and for referring to more complex datatypes such as structures. They are also essential if you want to use dynamic data in the free store area.  You won't always know the specific value in a pointer, but it does not matter as long as it contains the address of the variable you are after.

 You need to declare and initialize pointers just as you would other variables, but there are special operators that you need to use.

Pointer Operators.
Here is a table showing the special characters used in C++ to declare and use pointers. 

    *  Dereference           Operator Indirection Operator 

This is used to declare a variable as a pointer. 
It is also used when you want to access the value pointed to by the pointer variable. 

    &   Reference     Operator  Address-Of Operator 

Use before a variable to indicate that you mean the address of that variable. You'll often see this in a function header where the parameter list is given. 

    ->  Member Selection Operator  This is used to refer to members of structures.

Here is an example:

First, we'll declare an  integer, and then a pointer to that integer. 

int x;

int* p;

p= &x

The first part  int* p, tells the compiler to declare a pointer for integers and p will be the name of that pointer.   In the last part , &x; specifies that the address of the variable x is what should be assigned to the pointer variable. 

Try to visualize memory after these declarations, thinking of the pointer variable as not having a particular value, simply links to the variables to which they had been assigned.

Static Allocation Of Memory:

int x;                                                                                    

int* p;                                                                                 

int* q;

Memory for the pointer variables p and q, and the integer variable x is allocated at compilation time, that is before the program executes.

Dynamic Allocation Of Memory:

Memory allocation occurs at execution time. C++ enables dynamic allocation of memory by providing the operator new, which acts on a data type.

int* P;                                                                                    

P = new int;                                                              

The expression new int allocates a new memory cell that can contain an integer and returns a pointer to this new cell. The initial content of this new cell is undetermined.

Operator delete deallocates memory from a program, thus freeing the memory for future use by the program.

delete P;    Returns to the system the memory cell to which P points. It does not delete P itself.

If you no longer need the value in a pointer variable and you do not want the pointer to point to any particular memory cell, you assign NULL to the pointer variable. A NULL pointer value means that the pointer does not point to anything.

P = NULL

Suppose you no longer need a dynamically allocated memory cell. Simply changing all pointers to the cell wastes memory, because the cell remains allocated to the program, even though it is no longer accessible. This is called a memory leak.

To free space: 

delete Q;                                                                               

Q = NULL

 

Declaring pointer variables ptrType P, Q;

int  X;

Pointing to statically allocated memory P = &X;
Assigning a value *P = 6;
Allocating memory dynamically P = new int;
Assigning a value *P = 7;
Copying a pointer Q = P;
Allocating memory dynamically and assigning a value Q = new int;

*Q = 8;

Assigning NULL to a pointer variable P = NULL;
Deallocating memory delete Q;

Q = NULL;