SlideShare a Scribd company logo
1 of 17
P. Nivetha,
Dept. of BCA,
Bon Secours college for Women,
Thanjavur.
What is Pointer?
1.Pointer is a variable that holds a memory
address, usually location of another variable.
2.The Pointers are one of the C++’s most useful
and powerful features.
How Pointers are one of the C++’s
most useful and powerful features?
1.Pointer provide the means through which the memory
location of variable can be directly accessed and
hence it can be manipulated in the way as required.
2.Pointer supports C++ dynamic allocation routines.
3.Pointer can improve the efficiency of certain routines..
DYNAMIC AND STATIC
MEMORY ALLOCATION
The Golden Rule of computer states that
anything or everything that needs to be
processed must be loaded into internal
memory before its processing takes place.
Therefore the main memory is allocated in two
ways,
STATIC MEMORY ALLOCATION
DYNAMIC MEMORY ALLOCATION
STATIC MEMORY
ALLOCATION
In this technique the demanded memory is
allocated in advance that is at the compilation
time is called static memory allocation.
For example,
int s;
The compiler allocates the 2 bytes of memory
before its execution.
DYNAMIC MEMORY
ALLOCATION
In this technique the memory is allocated as
and when required during run time (program
execution) is called Dynamic memory
allocation.
C++ offers two types of operators called new
and delete to allocate and de-allocate the
memory at runtime.
FREE STORE
FREE STORE is a pool of unallocated heap
memory given to a program that is used by the
program for dynamic allocation during
execution.
DECLARATION AND
INITIALIZATION OF POINTERS
Pointer variables are declared like normal
variables except for the addition of unary
operator * (character)
The General Format of Pointer variable
declaration is ,
type * var_name;
where ,
type refers to any C++ valid data type and
var_name is any valid C++ variable
DECLARATION AND
INITIALIZATION OF POINTERS
For example,
int *iptr; // integer pointer variable points
to //another integer variable’s location.
float *fptr; // float type pointer variable points
to //another float type variable’s location.
char *cptr; // character type pointer variable
//points to another char type variable’s location.
POINTER AIRTHMETIC
Only two arithmetic operators, addition and
subtraction may be performed on pointers.
When you add 1 to a pointer, you are actually
adding the size of what ever the pointer
pointing at.
For example,
iptr++;
iptr--;
POINTER AIRTHMETIC
Only two arithmetic operators, addition and
subtraction may be performed on pointers.
When you add 1 to a pointer, you are actually
adding the size of what ever the pointer pointing
at.
For example,
iptr++;
iptr--;
Note: In pointer arithmetic all pointers increase
and decrease by the length of the data type they
point to.
DYNAMIC ALLOCATION OPERATORS -
new OPERATOR
C++ offers two types of operators called new and
delete to allocate and de-allocate the memory at
runtime. Since these two operators operate upon free
store memory, they are also called as free store
operators
Syntax :
pointer_variable = new data type;
where pointer_variable pointer variable and datatype
is valid C++ datatype and new is operator which
allocates the memory (size of data type) at run time.
For example,
int *iptr=new int;
char *cptr=new char;
float *fptr=new float;
Once a pointer points to newly allocated memory,
data values can be stored there using at address
operator
*cptr=‘a’;
*fptr=1.34;
*iptr=89;
DYNAMIC ALLOCATION OPERATORS -
new OPERATOR
Initializing values at the time of declaration one can
rewrite like,
int *iptr=new int(89);
char *cptr=new char(‘a’);
float *fptr=new float(1.34);
DYNAMIC ALLOCATION OPERATORS -
new OPERATOR
DYNAMIC ALLOCATION OPERATORS
CREATING DYNMIC ARRAYS 1D ARRAYS:
Syntax :
pointer_variable = new data type[size];
int *value=new int[10];
It will create memory space from the free store to
store 10 integers. Initilization while declaring dynamic
array is not possible but it is included in C++ compiler
ver 11.
CREATING DYNMIC ARRAYS (2D ARRAYS):
One needs to be tricky while defining 2D array as,
int *val,r,c,i,j;
cout<<“Enter dimensions”;
cin>>r>>c;
val=new int [ r * c ] ;
To read the elements of the 2D array
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>> val [ i *c + j ] ;
}
DYNAMIC ALLOCATION OPERATORS -
new OPERATOR
The life time of the variable or object created by new is not
restricted to the scope in which it is created. It lives in the
memory until explicitly deleted using the delete operator.
Syntax:
delete pointer_variable;
For example, delete iptr;
FOR ARRAYS :
Syntax :
delete [ ] arraypointer_variable;
delete [ ] val;
DYNAMIC ALLOCATION OPERATORS -
delete OPERATOR

More Related Content

What's hot

Pointer in c program
Pointer in c programPointer in c program
Pointer in c programRumman Ansari
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in CUri Dekel
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++Ilio Catallo
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppteShikshak
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in CTushar B Kute
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingAbdullah Jan
 
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMYComputer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMYklirantga
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 

What's hot (20)

Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in C
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Pointers
PointersPointers
Pointers
 
Dynamic Memory Allocation in C
Dynamic Memory Allocation in CDynamic Memory Allocation in C
Dynamic Memory Allocation in C
 
C pointer
C pointerC pointer
C pointer
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMYComputer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
 
C pointers
C pointersC pointers
C pointers
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 

Similar to Pointers

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.pptxsangeeta borde
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptchintuyadav19
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3sumitbardhan
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphismramya marichamy
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdfssusere19c741
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handlingRai University
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 

Similar to Pointers (20)

COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
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
 
Pointers
PointersPointers
Pointers
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
Session 5
Session 5Session 5
Session 5
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

Pointers

  • 1. P. Nivetha, Dept. of BCA, Bon Secours college for Women, Thanjavur.
  • 2. What is Pointer? 1.Pointer is a variable that holds a memory address, usually location of another variable. 2.The Pointers are one of the C++’s most useful and powerful features.
  • 3. How Pointers are one of the C++’s most useful and powerful features? 1.Pointer provide the means through which the memory location of variable can be directly accessed and hence it can be manipulated in the way as required. 2.Pointer supports C++ dynamic allocation routines. 3.Pointer can improve the efficiency of certain routines..
  • 4. DYNAMIC AND STATIC MEMORY ALLOCATION The Golden Rule of computer states that anything or everything that needs to be processed must be loaded into internal memory before its processing takes place. Therefore the main memory is allocated in two ways, STATIC MEMORY ALLOCATION DYNAMIC MEMORY ALLOCATION
  • 5. STATIC MEMORY ALLOCATION In this technique the demanded memory is allocated in advance that is at the compilation time is called static memory allocation. For example, int s; The compiler allocates the 2 bytes of memory before its execution.
  • 6. DYNAMIC MEMORY ALLOCATION In this technique the memory is allocated as and when required during run time (program execution) is called Dynamic memory allocation. C++ offers two types of operators called new and delete to allocate and de-allocate the memory at runtime.
  • 7. FREE STORE FREE STORE is a pool of unallocated heap memory given to a program that is used by the program for dynamic allocation during execution.
  • 8. DECLARATION AND INITIALIZATION OF POINTERS Pointer variables are declared like normal variables except for the addition of unary operator * (character) The General Format of Pointer variable declaration is , type * var_name; where , type refers to any C++ valid data type and var_name is any valid C++ variable
  • 9. DECLARATION AND INITIALIZATION OF POINTERS For example, int *iptr; // integer pointer variable points to //another integer variable’s location. float *fptr; // float type pointer variable points to //another float type variable’s location. char *cptr; // character type pointer variable //points to another char type variable’s location.
  • 10. POINTER AIRTHMETIC Only two arithmetic operators, addition and subtraction may be performed on pointers. When you add 1 to a pointer, you are actually adding the size of what ever the pointer pointing at. For example, iptr++; iptr--;
  • 11. POINTER AIRTHMETIC Only two arithmetic operators, addition and subtraction may be performed on pointers. When you add 1 to a pointer, you are actually adding the size of what ever the pointer pointing at. For example, iptr++; iptr--; Note: In pointer arithmetic all pointers increase and decrease by the length of the data type they point to.
  • 12. DYNAMIC ALLOCATION OPERATORS - new OPERATOR C++ offers two types of operators called new and delete to allocate and de-allocate the memory at runtime. Since these two operators operate upon free store memory, they are also called as free store operators Syntax : pointer_variable = new data type; where pointer_variable pointer variable and datatype is valid C++ datatype and new is operator which allocates the memory (size of data type) at run time.
  • 13. For example, int *iptr=new int; char *cptr=new char; float *fptr=new float; Once a pointer points to newly allocated memory, data values can be stored there using at address operator *cptr=‘a’; *fptr=1.34; *iptr=89; DYNAMIC ALLOCATION OPERATORS - new OPERATOR
  • 14. Initializing values at the time of declaration one can rewrite like, int *iptr=new int(89); char *cptr=new char(‘a’); float *fptr=new float(1.34); DYNAMIC ALLOCATION OPERATORS - new OPERATOR
  • 15. DYNAMIC ALLOCATION OPERATORS CREATING DYNMIC ARRAYS 1D ARRAYS: Syntax : pointer_variable = new data type[size]; int *value=new int[10]; It will create memory space from the free store to store 10 integers. Initilization while declaring dynamic array is not possible but it is included in C++ compiler ver 11.
  • 16. CREATING DYNMIC ARRAYS (2D ARRAYS): One needs to be tricky while defining 2D array as, int *val,r,c,i,j; cout<<“Enter dimensions”; cin>>r>>c; val=new int [ r * c ] ; To read the elements of the 2D array for(i=0;i<r;i++) { for(j=0;j<c;j++) cin>> val [ i *c + j ] ; } DYNAMIC ALLOCATION OPERATORS - new OPERATOR
  • 17. The life time of the variable or object created by new is not restricted to the scope in which it is created. It lives in the memory until explicitly deleted using the delete operator. Syntax: delete pointer_variable; For example, delete iptr; FOR ARRAYS : Syntax : delete [ ] arraypointer_variable; delete [ ] val; DYNAMIC ALLOCATION OPERATORS - delete OPERATOR