Post on 04-Apr-2018
7/29/2019 12438 18Types Pointers
1/16
Types of Pointers
7/29/2019 12438 18Types Pointers
2/16
Pointer Arithmetic
Various operations: -
Addition of a number to a pointer variable.
Subtraction of a number from a pointervariable.
Subtraction of one pointer variable from
another.
7/29/2019 12438 18Types Pointers
3/16
Addition of a number to apointer 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 forlong int, & 8 for long float.
7/29/2019 12438 18Types Pointers
4/16
Subtraction of a number froma pointer variable
Suppose p is a pointer variable pointing to an
element of integer type, then the statement
p--; or --p;
decrements the value of p by a factor of 2.
7/29/2019 12438 18Types Pointers
5/16
Subtraction of one pointervariable 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.
7/29/2019 12438 18Types Pointers
6/16
Pointer to a Pointer
We can have a variable that holds an
address of a variable that in turn holds an
address of another variablepointer to a
pointer or double indirection.
Declaration: -
int **ptr;
7/29/2019 12438 18Types Pointers
7/16
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 dynamicallyat run time.
We can also pass a function as an argument
to another function. Function is passed as a
pointer.
7/29/2019 12438 18Types Pointers
8/16
Contd
Two types of function pointers: -
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) ();
7/29/2019 12438 18Types Pointers
9/16
Example:
typedef void (*FunPtr) (int, int);
void Add (int i, int j){ cout
7/29/2019 12438 18Types Pointers
10/16
This Pointer
C++ uses a unique keyword called thisto 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.
7/29/2019 12438 18Types Pointers
11/16
Example:-class sample
{ int a, b;
public:void showAddress()
{ cout
7/29/2019 12438 18Types Pointers
12/16
Dynamic Memory Management
C++ provides a set of operators, calleddynamic 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 itsexecution.
If you run the same program many times,many users are using your programconcurrently, the OS may run out of memory.
7/29/2019 12438 18Types Pointers
13/16
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];
7/29/2019 12438 18Types Pointers
14/16
Contd
It permits the initialization of memory locationsduring allocation.
Syntax: -
type *ptrVar = new type (IntialValue);
Example: - int *intPtr = new int (100);
This statement allocates memory for an integernumber & initializes it with value 100. The addressof the memory allocated memory is assigned topointer variable intPtr.
7/29/2019 12438 18Types Pointers
15/16
Delete Operator
A counterpart of new operator & it de-
allocates memory allocated by the new
operator back to the free poll of memory.
Syntax: -
delete ptrVar;
7/29/2019 12438 18Types Pointers
16/16
Thank You!!!