SlideShare a Scribd company logo
POINTERS
Prof. K. Adisesha
Learning Outcomes
 Introduction
 Defining Pointer
 Memory Utilization
 Pointer Declaration
 Pointer Arithmetic
 Pointers as Function Parameters
 Dynamic Allocation of Memory
2
Introduction
Pointers
 Pointers are a powerful concept in C++ and have the
following advantages.
 i. It is possible to write efficient programs.
 ii. Memory is utilized properly.
 iii. Dynamically allocate and de-allocate memory.
 Declaration of a variable tells the compiler to perform the
following.
 Allocate a location in memory. The number of location depends
on data type.
 Establish relation between address of the location and the name
of the variable
3
Pointers
Definition:
 A pointer is a variable that holds a memory address of
another variable.
 The pointer has the following advantages.
 Pointers save memory space.
 Dynamically allocate and de-allocate memory.
 Easy to deal with hardware components.
 Establishes communication between program and data.
 Pointers are used for file handling.
 Pointers are used to create complex data structures such as
linked list, stacks, queues trees and graphs.
4
Pointers
Pointer Declaration:
 Pointers are also variables and hence, they must be defined in a
program like any other variable.
 The general syntax of pointer declaration is given below.
 Syntax:
Data_Type *Ptr_Varname;
 Where,
Data_Type is any valid data type supported by C++ or any user
defined type.
Ptr_Varname is the name of the pointer variable.
The presence of ‘*’ indicates that it is a pointer variable.
5
Pointers
Defining pointer variables:
 int *iptr; iptr is declared to be a pointer variable of int type.
 float *fptr; fptr is declared to be a pointer variable of float type.
 char *cptr; cptr is declared to be a pointer variable of character
type.
Pointer Initialization:
 Once we declare a pointer variable, we must make it to point to
something.
 Initializing to the pointer the address of the variable you want to
point to as in:
iptr = #
 The ‘&’ is the address operator and it represents address of the
variable 6
Pointers
The address of operator (&):
 “&‟ is a unary operator called as ‘the address-of’
operator that returns the memory address of its
operand.
 Example:
 if ‘num’ is an integer variable, then ‘&num’ is its address.
 Example:
int num = 25;
int *iptr;
iptr = # //The address of operator &
7
Pointers
Pointer Operator or Indirection Operator (*):
 The second operator is indirection operator ‘*’, and it is the
complement of ‘&’.
 It is a unary operator that returns the value of the variable
located at the address specified by its operand.
 Example:
int num = 25;
int *iptr; //Pointer Operator (Indirection Operator *)
iptr = #
8
Pointers
Pointer Arithmetic:
 We can perform arithmetic operations on a pointer just as you
can a numeric value.
 There are four arithmetic operators that can be used on
pointers:
 Increment ++
 Decrement --
 Addition +
 Subtraction -
9
Pointers
Pointer Arithmetic:
 The following operation can be performed on pointers.
 We can add integer value to a pointer.
 We can subtract an integer value from a pointer.
 We can compare two pointers, if they point the elements of
the same array.
 We can subtract one pointer from another pointer if both
point to the same array.
 We can assign one pointer to another pointer provided both
are of same type.
10
Pointers
Pointer Arithmetic:
 The following operations cannot be performed on
pointers.
 Addition of two pointers.
 Subtraction of one pointer from another pointer when they
do not point to the same array.
 Multiplication of two pointers.
 Division of two pointers.
11
Pointers
Array of Pointers:
 As we have an array of integers, array of float, similarly there
can be an array of pointers.
 “An array of pointer means that it is a collection of address”.
 The general form of array of pointers declaration is:
int *pint[5];
 The above statement declares an array of 5 pointers where each
of the pointer to integer variable.
12
Pointers
Pointers and Functions:
 A function may be invoked in one of two ways :
 Call by value
 Call by reference
 Function call by reference can be used in two ways :
 By passing the references
 By passing the pointers
 Reference is an alias name for a variable.
 Example:
13
int m = 23;
int &n = m;
int *p;
p = &m;
Pointers
Pointers as Function Parameters:
 A pointer can be used as Function parameter.
 It works like a reference parameter to allow change to
argument from within the function.
 Example:
14
void swap(int *m, int *n)
{
int temp;
temp = *m;
*m = *n;
*n = temp;
}
void swap(&num1, & num2);
Pointers
Invoking Function by Passing the References:
 When parameters are passed to the functions by reference, then the
formal parameters become references to the actual parameters to the
calling function.
 The called function does not create its own copy of original values,
rather, it refers to the values by their references.
 Example:
15
#include<iostream.h>
void main()
{
void swap(int &, int &);
int a = 10, b = 20;
cout << “n Value of a &b:” << a << b;
swap(a, b);
cout << “n After swapping a &b :” << a <<b;
}
void swap(int &m, int &n)
{
int temp;
temp = m;
m = n;
n = temp;
}
Pointers
Invoking Function by Passing the Pointers:
 When the pointers are passed to the function, the addresses of actual
arguments in the calling function are copied into formal arguments
of the called function.
 The formal arguments in the called function, can make changing the
actual arguments of the calling function..
 Example:
16
#include<iostream.h>
void main()
{
void swap(int *m, int *n);
int a = 10, b = 20;
cout << “n Value of a, b :” << a << b;
swap(&a, &b);
cout << “n After swapping a, b :” << a <<b;
}
void swap(int *m, int *n)
{
int temp;
temp = *m;
*m = *n;
*n = temp;
}
Memory Allocation
Dynamic Allocation of Memory:
 Dynamic memory allocation refers to the process of allocating
memory during the execution of the program or at run time.
 Memory space allocated with this method is not fixed.
 C++ supports dynamic allocation and de-allocation of objects
using:
 new operators.
 delete operators
 These operators allocate memory for objects from a pool called
the free store.
 The new operator calls the special function operator new and
delete operators call the special function operator delete.
17
Memory Allocation
new operator:
 We can allocate storage for a variable while program is
running by using new operator.
 Example:
 To allocate memory for variable:
int *pnum;
pnum = new int;
 The first line declares the pointer pnum.
 The second line then allocates memory for an integer
 To allocate memory for array:
double *dptr = new double[25];
18
Memory Allocation
delete Operator:
 The delete operator is used to destroy the variables space
which has been created by using the new operator dynamically.
 Example:
 Use delete operator to free dynamic memory as:
delete iptr;
 To free dynamic array memory.
delete [] dptr;
 To free dynamic structure, delete structure:
delete structure;
19
Memory Allocation
Difference between Static and Dynamic Memory
Allocation:
20
Static Memory Allocation Dynamic Memory Allocation
Memory space is allocated before
the execution of program.
Memory space is allocated during
the execution of program.
Memory space allocated is fixed Memory space allocated is not fixed
More memory space is required. Less memory space is required.
Memory allocation from stack area. Memory space form heap area.
Memory Allocation
Free store (Heap memory):
 Free store is a pool of memory available to allocated and de-
allocated storage for the objects during the execution of the
memory.
Memory Leak:
 If the objects, that are allocated memory dynamically, are not
deleted using delete, the memory block remains occupied even
at the end of the program.
 Such memory blocks are known as orphaned memory blocks.
 These orphaned memory blocks when increases in number,
bring adverse effect on the system.
 This situation is called memory leak
21
Memory Allocation
Pointers and Structures:
 We can create pointers to structures variable.
struct student {int roll_no; float fee; };
student s;
student * sp = &s;
(*sp).roll_no = 14;
 The above statement can be written using the operator as
Sp *roll_no = 14;
Pointers and Objects:
 The Pointers pointing to objects are referred to as object
pointers. class_name * object-pointer;
 Here class_name is the name of the class, object-pointer is the pointer
to an object of this class type.
22
Memory Allocation
this pointers:
 Every object in C++ has access to its own address through an
important pointer called this pointer.
 The “this pointer” is an implicit parameter to all member
functions.
 Therefore, inside a member function, this may be used to refer
to the invoking object.
 Example:
void main( )
{
Student *S; // Create a pointer to point Student object
S->readdata( ); // Access Student data member using a pointer
S->display( ); // Display data using a pointer
}
23

More Related Content

Pointers

  • 2. Learning Outcomes  Introduction  Defining Pointer  Memory Utilization  Pointer Declaration  Pointer Arithmetic  Pointers as Function Parameters  Dynamic Allocation of Memory 2
  • 3. Introduction Pointers  Pointers are a powerful concept in C++ and have the following advantages.  i. It is possible to write efficient programs.  ii. Memory is utilized properly.  iii. Dynamically allocate and de-allocate memory.  Declaration of a variable tells the compiler to perform the following.  Allocate a location in memory. The number of location depends on data type.  Establish relation between address of the location and the name of the variable 3
  • 4. Pointers Definition:  A pointer is a variable that holds a memory address of another variable.  The pointer has the following advantages.  Pointers save memory space.  Dynamically allocate and de-allocate memory.  Easy to deal with hardware components.  Establishes communication between program and data.  Pointers are used for file handling.  Pointers are used to create complex data structures such as linked list, stacks, queues trees and graphs. 4
  • 5. Pointers Pointer Declaration:  Pointers are also variables and hence, they must be defined in a program like any other variable.  The general syntax of pointer declaration is given below.  Syntax: Data_Type *Ptr_Varname;  Where, Data_Type is any valid data type supported by C++ or any user defined type. Ptr_Varname is the name of the pointer variable. The presence of ‘*’ indicates that it is a pointer variable. 5
  • 6. Pointers Defining pointer variables:  int *iptr; iptr is declared to be a pointer variable of int type.  float *fptr; fptr is declared to be a pointer variable of float type.  char *cptr; cptr is declared to be a pointer variable of character type. Pointer Initialization:  Once we declare a pointer variable, we must make it to point to something.  Initializing to the pointer the address of the variable you want to point to as in: iptr = &num;  The ‘&’ is the address operator and it represents address of the variable 6
  • 7. Pointers The address of operator (&):  “&‟ is a unary operator called as ‘the address-of’ operator that returns the memory address of its operand.  Example:  if ‘num’ is an integer variable, then ‘&num’ is its address.  Example: int num = 25; int *iptr; iptr = &num; //The address of operator & 7
  • 8. Pointers Pointer Operator or Indirection Operator (*):  The second operator is indirection operator ‘*’, and it is the complement of ‘&’.  It is a unary operator that returns the value of the variable located at the address specified by its operand.  Example: int num = 25; int *iptr; //Pointer Operator (Indirection Operator *) iptr = &num; 8
  • 9. Pointers Pointer Arithmetic:  We can perform arithmetic operations on a pointer just as you can a numeric value.  There are four arithmetic operators that can be used on pointers:  Increment ++  Decrement --  Addition +  Subtraction - 9
  • 10. Pointers Pointer Arithmetic:  The following operation can be performed on pointers.  We can add integer value to a pointer.  We can subtract an integer value from a pointer.  We can compare two pointers, if they point the elements of the same array.  We can subtract one pointer from another pointer if both point to the same array.  We can assign one pointer to another pointer provided both are of same type. 10
  • 11. Pointers Pointer Arithmetic:  The following operations cannot be performed on pointers.  Addition of two pointers.  Subtraction of one pointer from another pointer when they do not point to the same array.  Multiplication of two pointers.  Division of two pointers. 11
  • 12. Pointers Array of Pointers:  As we have an array of integers, array of float, similarly there can be an array of pointers.  “An array of pointer means that it is a collection of address”.  The general form of array of pointers declaration is: int *pint[5];  The above statement declares an array of 5 pointers where each of the pointer to integer variable. 12
  • 13. Pointers Pointers and Functions:  A function may be invoked in one of two ways :  Call by value  Call by reference  Function call by reference can be used in two ways :  By passing the references  By passing the pointers  Reference is an alias name for a variable.  Example: 13 int m = 23; int &n = m; int *p; p = &m;
  • 14. Pointers Pointers as Function Parameters:  A pointer can be used as Function parameter.  It works like a reference parameter to allow change to argument from within the function.  Example: 14 void swap(int *m, int *n) { int temp; temp = *m; *m = *n; *n = temp; } void swap(&num1, & num2);
  • 15. Pointers Invoking Function by Passing the References:  When parameters are passed to the functions by reference, then the formal parameters become references to the actual parameters to the calling function.  The called function does not create its own copy of original values, rather, it refers to the values by their references.  Example: 15 #include<iostream.h> void main() { void swap(int &, int &); int a = 10, b = 20; cout << “n Value of a &b:” << a << b; swap(a, b); cout << “n After swapping a &b :” << a <<b; } void swap(int &m, int &n) { int temp; temp = m; m = n; n = temp; }
  • 16. Pointers Invoking Function by Passing the Pointers:  When the pointers are passed to the function, the addresses of actual arguments in the calling function are copied into formal arguments of the called function.  The formal arguments in the called function, can make changing the actual arguments of the calling function..  Example: 16 #include<iostream.h> void main() { void swap(int *m, int *n); int a = 10, b = 20; cout << “n Value of a, b :” << a << b; swap(&a, &b); cout << “n After swapping a, b :” << a <<b; } void swap(int *m, int *n) { int temp; temp = *m; *m = *n; *n = temp; }
  • 17. Memory Allocation Dynamic Allocation of Memory:  Dynamic memory allocation refers to the process of allocating memory during the execution of the program or at run time.  Memory space allocated with this method is not fixed.  C++ supports dynamic allocation and de-allocation of objects using:  new operators.  delete operators  These operators allocate memory for objects from a pool called the free store.  The new operator calls the special function operator new and delete operators call the special function operator delete. 17
  • 18. Memory Allocation new operator:  We can allocate storage for a variable while program is running by using new operator.  Example:  To allocate memory for variable: int *pnum; pnum = new int;  The first line declares the pointer pnum.  The second line then allocates memory for an integer  To allocate memory for array: double *dptr = new double[25]; 18
  • 19. Memory Allocation delete Operator:  The delete operator is used to destroy the variables space which has been created by using the new operator dynamically.  Example:  Use delete operator to free dynamic memory as: delete iptr;  To free dynamic array memory. delete [] dptr;  To free dynamic structure, delete structure: delete structure; 19
  • 20. Memory Allocation Difference between Static and Dynamic Memory Allocation: 20 Static Memory Allocation Dynamic Memory Allocation Memory space is allocated before the execution of program. Memory space is allocated during the execution of program. Memory space allocated is fixed Memory space allocated is not fixed More memory space is required. Less memory space is required. Memory allocation from stack area. Memory space form heap area.
  • 21. Memory Allocation Free store (Heap memory):  Free store is a pool of memory available to allocated and de- allocated storage for the objects during the execution of the memory. Memory Leak:  If the objects, that are allocated memory dynamically, are not deleted using delete, the memory block remains occupied even at the end of the program.  Such memory blocks are known as orphaned memory blocks.  These orphaned memory blocks when increases in number, bring adverse effect on the system.  This situation is called memory leak 21
  • 22. Memory Allocation Pointers and Structures:  We can create pointers to structures variable. struct student {int roll_no; float fee; }; student s; student * sp = &s; (*sp).roll_no = 14;  The above statement can be written using the operator as Sp *roll_no = 14; Pointers and Objects:  The Pointers pointing to objects are referred to as object pointers. class_name * object-pointer;  Here class_name is the name of the class, object-pointer is the pointer to an object of this class type. 22
  • 23. Memory Allocation this pointers:  Every object in C++ has access to its own address through an important pointer called this pointer.  The “this pointer” is an implicit parameter to all member functions.  Therefore, inside a member function, this may be used to refer to the invoking object.  Example: void main( ) { Student *S; // Create a pointer to point Student object S->readdata( ); // Access Student data member using a pointer S->display( ); // Display data using a pointer } 23