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

What's hot

C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
Anil Bapat
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
Dhrumil Patel
 
C pointer
C pointerC pointer
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Structure in c
Structure in cStructure in c
Structure in c
Prabhu Govind
 
Enums in c
Enums in cEnums in c
C functions
C functionsC functions
C Pointers
C PointersC Pointers
C Pointers
omukhtar
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Anil Pokhrel
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
hydpy
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-c
teach4uin
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Array and string
Array and stringArray and string
Array and string
prashant chelani
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
HalaiHansaika
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
Selvaraj Seerangan
 
File handling in C
File handling in CFile handling in C
File handling in C
Kamal Acharya
 

What's hot (20)

C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
C pointer
C pointerC pointer
C pointer
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Structure in c
Structure in cStructure in c
Structure in c
 
Enums in c
Enums in cEnums in c
Enums in c
 
C functions
C functionsC functions
C functions
 
C Pointers
C PointersC Pointers
C Pointers
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-c
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Array and string
Array and stringArray and string
Array and string
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
File handling in C
File handling in CFile handling in C
File handling in C
 

Similar to Pointers

Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
8 Pointers
8 Pointers8 Pointers
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
ShivanshuVerma11
 
Pointer
PointerPointer
Pointer
manish840
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
study material
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Vijayananda Ratnam Ch
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
Prerna Sharma
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
JoeyDelaCruz22
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
Sabaunnisa3
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
sangeeta borde
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
ssusere19c741
 
cprogrammingpointerstype.ppt
cprogrammingpointerstype.pptcprogrammingpointerstype.ppt
cprogrammingpointerstype.ppt
akila m
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene1
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 

Similar to Pointers (20)

Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
 
Pointer
PointerPointer
Pointer
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
cprogrammingpointerstype.ppt
cprogrammingpointerstype.pptcprogrammingpointerstype.ppt
cprogrammingpointerstype.ppt
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 

More from Prof. Dr. K. Adisesha

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
Prof. Dr. K. Adisesha
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
Prof. Dr. K. Adisesha
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Prof. Dr. K. Adisesha
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Prof. Dr. K. Adisesha
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Prof. Dr. K. Adisesha
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Prof. Dr. K. Adisesha
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
Prof. Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
Prof. Dr. K. Adisesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
Prof. Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
Prof. Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
Prof. Dr. K. Adisesha
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
Prof. Dr. K. Adisesha
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
Prof. Dr. K. Adisesha
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
Prof. Dr. K. Adisesha
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
Prof. Dr. K. Adisesha
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
Prof. Dr. K. Adisesha
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
Prof. Dr. K. Adisesha
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
Prof. Dr. K. Adisesha
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
Prof. Dr. K. Adisesha
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
Prof. Dr. K. Adisesha
 

More from Prof. Dr. K. Adisesha (20)

Software Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdfSoftware Engineering notes by K. Adisesha.pdf
Software Engineering notes by K. Adisesha.pdf
 
Software Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdfSoftware Engineering-Unit 1 by Adisesha.pdf
Software Engineering-Unit 1 by Adisesha.pdf
 
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdfSoftware Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
Software Engineering-Unit 2 "Requirement Engineering" by Adi.pdf
 
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdfSoftware Engineering-Unit 3 "System Modelling" by Adi.pdf
Software Engineering-Unit 3 "System Modelling" by Adi.pdf
 
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdfSoftware Engineering-Unit 4 "Architectural Design" by Adi.pdf
Software Engineering-Unit 4 "Architectural Design" by Adi.pdf
 
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdfSoftware Engineering-Unit 5 "Software Testing"by Adi.pdf
Software Engineering-Unit 5 "Software Testing"by Adi.pdf
 
Computer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. AdiseshaComputer Networks Notes by -Dr. K. Adisesha
Computer Networks Notes by -Dr. K. Adisesha
 
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. AdiaeshaCCN Unit-1&2 Data Communication &Networking by K. Adiaesha
CCN Unit-1&2 Data Communication &Networking by K. Adiaesha
 
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. AdiseshaCCN Unit-3 Data Link Layer by Dr. K. Adisesha
CCN Unit-3 Data Link Layer by Dr. K. Adisesha
 
CCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. AdiseshaCCN Unit-4 Network Layer by Dr. K. Adisesha
CCN Unit-4 Network Layer by Dr. K. Adisesha
 
CCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdfCCN Unit-5 Transport & Application Layer by Adi.pdf
CCN Unit-5 Transport & Application Layer by Adi.pdf
 
Introduction to Computers.pdf
Introduction to Computers.pdfIntroduction to Computers.pdf
Introduction to Computers.pdf
 
R_Programming.pdf
R_Programming.pdfR_Programming.pdf
R_Programming.pdf
 
Scholarship.pdf
Scholarship.pdfScholarship.pdf
Scholarship.pdf
 
Operating System-2 by Adi.pdf
Operating System-2 by Adi.pdfOperating System-2 by Adi.pdf
Operating System-2 by Adi.pdf
 
Operating System-1 by Adi.pdf
Operating System-1 by Adi.pdfOperating System-1 by Adi.pdf
Operating System-1 by Adi.pdf
 
Operating System-adi.pdf
Operating System-adi.pdfOperating System-adi.pdf
Operating System-adi.pdf
 
Data_structure using C-Adi.pdf
Data_structure using C-Adi.pdfData_structure using C-Adi.pdf
Data_structure using C-Adi.pdf
 
JAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdfJAVA PPT -2 BY ADI.pdf
JAVA PPT -2 BY ADI.pdf
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 

Recently uploaded

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 

Recently uploaded (20)

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 

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