You are on page 1of 16

# Types of Pointers

Pointer Arithmetic
Various operations: Addition of a number to a pointer variable. Subtraction of a number from a pointer variable. Subtraction of one pointer variable from another.

## Addition of a number to a pointer variable

Suppose p is a pointer variable pointing to an element of integer type, then the statement

p++; or ++p; increments the value of p by a factor of 2. This increment factor will be 1 for char, 4 for long int, & 8 for long float.

## Subtraction of a number from a pointer variable

Suppose p is a pointer variable pointing to an element of integer type, then the statement

## Subtraction of one pointer variable from another

One pointer variable can be subtracted from another provided both point to the same data type. Operations on pointers are not permitted:

Addition of two pointer variables. Multiplication of a pointer variable by a number. Division of a pointer variable by a number.

Pointer to a Pointer

We can have a variable that holds an address of a variable that in turn holds an address of another variable pointer to a pointer or double indirection. Declaration: int **ptr;

Pointers to Functions

Used in C++ for dynamic binding, & event based applications. Known as callback function. Using it, we can allow a C++ program to select a function dynamically at run time. We can also pass a function as an argument to another function. Function is passed as a pointer.

Contd

## Point to static member functions Point to non-static member functions

These two function pointers are incompatible with each other. 2nd type requires hidden argument. Syntax: - data_type(*function_name) ();

Example:
typedef void (*FunPtr) (int, int); void Add (int i, int j) { cout<< i << + << j << = << i + j ; } void Subtract (int i, int j); { cout<< i << - << j << = << i - j ; } void main() { FunPtr ptr; ptr = &Add; ptr (1,2); cout<<endl; ptr = &Subtract; ptr(3,2); }

This Pointer

C++ uses a unique keyword called this to represent an object that invoked a member function. All non-static member functions of an object have access to a special pointer named this. The this pointer hold the address of the object whose member function is invoked. It is not a part of an object. It is passed as an implicit first argument to every non-static member function.

## Dynamic Memory Management

C++ provides a set of operators, called dynamic memory management operators, to allocate & de-allocate memory at execution time, i.e. dynamically. The memory allocated dynamically, must be de-allocated before your program finishes its execution. If you run the same program many times, many users are using your program concurrently, the OS may run out of memory.

New Operator
Allocates the memory & always returns a pointer to an appropriate type. Syntax: type * new type [size in integer]; Example: int *intPtr; intPtr = new int [100];

Contd

It permits the initialization of memory locations during allocation. Syntax: type *ptrVar = new type (IntialValue);

Example: - int *intPtr = new int (100); This statement allocates memory for an integer number & initializes it with value 100. The address of the memory allocated memory is assigned to pointer variable intPtr.

Delete Operator

A counterpart of new operator & it deallocates memory allocated by the new operator back to the free poll of memory. Syntax: delete ptrVar;

Thank You!!!