Chapter-11 Pointers: Introduction
Chapter-11 Pointers: Introduction
Chapter-11 Pointers: Introduction
Chapter-11
POINTERS
Introduction:
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.
Pointer:
A pointer is a variable that holds a memory address of another variable.
The pointer has the following advantages.
o Pointers save memory space.
o Dynamically allocate and de-allocate memory.
Important
o Easy to deal with hardware components.
3 Marks
o Establishes communication between program and data.
o Pointers are used for file handling.
o Pointers are used to create complex data structures such as linked list, stacks, queues trees
and graphs.
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_Variablename;
Where,
o Data_Type is any valid data type supported by C++ or any user defined type.
o Ptr_Variablename is the name of the pointer variable. The presence of ‘*’ indicates that it
is a pointer variable.
Defining pointer variables:
o int *iptr; iptr is declared to be a pointer variable of int type.
o float *fptr; fptr is declared to be a pointer variable of float type.
o 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.
We can do this by assigning or 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.
Example: A program to display the content of num and the address of the variable num using a
pointer variable.
#include<iostream.h>
void main( )
{
int num; // normal integer variable
int *iptr; // Pointer declaration, pointing to integer data
var”.
Example:
int num = 25;
int *iptr;
iptr = # //The address of operator &
num = 300;
iptr = & num;
val = *iptr; OUTPUT:
}
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:
o Increment ++
o Decrement --
o Addition +
o Subtraction -
Example:
int num, *iptr; iptr 1200 9
num = 9; 1201
iptr = # iptr++ 1202
iptr++; 1203
cout<<iptr;
The following operation can be performed on pointers.
o We can add integer value to a pointer.
o We can subtract an integer value from a pointer.
o We can compare two pointers, if they point the elements of the same array.
o We can subtract one pointer from another pointer if both point to the same array.
o We can assign one pointer to another pointer provided both are of same type.
The following operations cannot be performed on pointers.
o Addition of two pointers.
o Subtraction of one pointer from another pointer when they do not point to the same array.
o Multiplication of two pointers.
o Division of two pointers.
A program to illustrate the pointer expression and pointer arithmetic.
#include<iostream.h>
#include<conio.h>
void main( )
{ OUTPUT:
int a, b, x, y; Address of a = 65524
int *ptr1, *ptr2; Address of b = 65522
a = 30;
b = 6; a = 30 b=6
ptr1 = &a x = 30 y=6
ptr2 = &b; a = 100 b = 12
x = *ptr1 + *ptr2 – 6;
y = 6 - *ptr1 / *ptr2 + 30;
cout<<”Address of a = “<<ptr1<<endl;
cout<<”Address of b = “<<ptr2<<endl;
cout<<”a = “<<a<<”b = “<<b<<endl;
cout<<”x = “<<x<<”y = “<<y<<endl;
int a[10], i, n;
cout<<”Enter the input for array”;
cin>>n;
cout<<”Enter array elements:”; OUTPUT:
for(i=0; i<n; i++)
Enter the input for array 5
cin>>*(a+i);
cout<<The given array elements are :”; Enter array elements
for(i=0; i<n; i++) 1 2 3 4 5
cout<<”\t”<<*(a+i);
The given array elements are :
getch( );
} 1 2 3 4 5
Array of Pointers:
There is 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.
Example: Program to illustrate the array of pointers of isolated variables.
#include<iostream.h>
#include<conio.h>
void main( )
{
int *pint[5];
OUTPUT:
int a = 10, b = 20, c = 30, d=40, e=50;
pint[0] = &a; Value 10 stored at 17500
pint[1] = &b; Value 20 stored at 17502
pint[2] = &c; Value 30 stored at 17504
pint[3] = &d;
Value 40 stored at 17506
pint[4] = &e;
Value 50 stored at 17508
for( int i=0; i<=4; i++)
cout<<”Value “ << *pint[i]<< “stored at “<<pint[i]<<endl;
}
void main()
{
void swap(int *m, int *n);
int a = 5, b = 6;
cout << “\n Value of a :” << a << “ and b :” << b;
swap(&a, &b);
cout << “\n After swapping value of a :” << a << “and b :” << b;
}
void swap(int *m, int *n)
{
int temp; OUTPUT:
temp = *m; Value of a : 5 and b : 6
*m = *n;
After swapping value of a : 6 and b : 5
*n = temp;
}
Memory Allocation of pointers:
Memory Allocation is done in two ways:
Important
o Static Allocation of memory
3 Marks
o Dynamic allocation of memory.
new operator:
We can allocate storage for a variable while program is running by using new operator.
It is used to allocate memory without having to define variables and then make pointers point to
them.
9|Page Keerthi Kumar H M
Chapter 11- Pointers II PUC, MDRPUC, Hassan
The following code demonstrates how to allocate memory for different variables.
To allocate memory type integer
int *pnumber;
pnumber = new int;
The first line declares the pointer, pnumber. The second line then allocates memory for an integer
and then makes pnumber point to this new memory.
To allocate memory for array, double *dptr = new double[25];
To allocates dynamic structure variables or objects, student sp = new student;
delete Operator:
The delete operator is used to destroy the variables space which has been created by using the
new operator dynamically.
Use delete operator to free dynamic memory as : delete iptr;
To free dynamic array memory: delete [] dptr;
To free dynamic structure, delete structure;
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.
10 | P a g e Keerthi Kumar H M
Chapter 11- Pointers II PUC, MDRPUC, Hassan
cout<<”Employee Number:”<<empno;
cout<<”Employee Name:”<<name;
cout<<”Employee Salary:”<<salary;
}
};
void main( )
{
employee e, * ep;
ep = &e;
clrscr( );
ep read( );
ep display ( );
getch( );
}
Here, employee is an already defined class. When accessing members of the class an object
pointer, the arrow operator () is used instead of dot (.) operator.
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.
Program 12: Create a class containing the following data members Register_No, Name and
Fees. Also create a member function to read and display the data using the concept of pointers to
objects.
#include<iostream.h>
#include<conio.h>
class Student
{
private:
long regno;
char name[20];
float fees;
public:
void readdata( );
void display( );
};
void Student::readdata( )
{
cout<<"Enter the Register Number:"<<endl;
cin>>regno;
12 | P a g e Keerthi Kumar H M
Chapter 11- Pointers II PUC, MDRPUC, Hassan
Important Questions
1 Marks Question:
1. Define pointer. [June 2016]
2. Write the declaration syntax for a pointer. [March 2015]
3. How do we initialize a pointer? [March 2016]
4. Write any one advantage of pointers. [June 2015, March 2017]
5. What is the purpose of new operator in C++? [June 2017]
6. What is a pointer variable?
13 | P a g e Keerthi Kumar H M
Chapter 11- Pointers II PUC, MDRPUC, Hassan
3 Marks Question:
1. What are the advantages of pointers? [June 2016]
2. What are the operations performed on pointers? [March 2015, June 2017]
3. What is array of pointer? Give example. [June 2015]
4. Explain the new and delete operator in pointers. [March 2016]
5. Define: [March 2017]
a. Pointer.
b. Static memory allocation.
c. Dynamic memory allocation
6. What is the relationship between pointers and arrays?
7. Explain with example call by reference.
8. Distinguish between static and dynamic memory allocation.
9. What is the relationship between pointers and objects? Give an example
10. Explain the use of “this pointer”.
****************
14 | P a g e Keerthi Kumar H M