SlideShare a Scribd company logo
WEL COME
PRAVEEN M JIGAJINNI
PGT (Computer Science)
MCA, MSc[IT], MTech[IT], MPhil (Comp. Sci), PGDCA, ADCA,
Dc. Sc. & Engg.
Reference Book
CLASS XII
By
Sumita Arora
CHAPTER 08
POINTERS
What is Pointer?
Pointer is a variable that holds a
memory address, usually location of
another variable.
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?
First : 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.
Second: Pointer supports C++ dynamic
allocation routines.
Third: Pointer can improve the
efficiency of certain routines.
How Pointers are one of the C++’s
most useful and powerful
features?
C++ MEMORY MAP
STACK
HEAP
GLOBAL
VARIABLES
PROGRAM
CODE
STACK - This area is used for
function calls return address,
argument and local variables
HEAP – This area is used for
Dynamic Memory Allocation
GLOBAL VARIABLES - This
area is used to store global
variables where they exists as long
as the program continues
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.
Two Special Operators
Two special operators are used * and & are
with pointers.
& operator is called as address of operator
which provides the address of operand.
* Operator is called as at address and also
called as differencing which provides the
value being pointed by pointer.
Two Special Operators
For example,
int i = 25;
int *iptr;
iptr= &i;
25i
1050
1050iptr
NULL Pointer (ZERO POINTER)
A pointer variable must not remain uninitilized
since uninitialized pointer cause system crash.
Even if you do not have legal pointer value to
initialize a pointer, you can initialize it with
NULL pointer (ZERO POINTER).
int *iptr=NULL;
Note: In C++ version 11 nullptr keyword is
introduced to assign null. nullptr is legal empty
null pointer.
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
Improper use of new and delete may lead to memory
leaks. Make sure that the memory allocated through new
must be deleted through delete.
Otherwise this may lead to adverse effect on the
system
DYNAMIC ALLOCATION
OPERATORS – MEMORY LEAK
It is faster to use an element pointer than an index when
scanning arrays,
For 1D Array,
S[0]= *( S + 0 ) or *S or *(S)
S[1] = *( S + 1 )
S[2] = *( S + 2 )
FOR 2D Arrays,
S [ 0 ] [ 0 ] = *( S [ 0 ] + 0 ) or * ( * ( S + 0 ) )
S [ 0 ] [ 1 ] = *( S [ 0 ] + 1 ) or * ( * ( S + 1 ) )
S [ 1 ] [ 2 ] = *( S [ 1 ] + 2 ) or * ( * ( S + 1 ) +
2 )
ARRAYS
POINTERS AND STRINGS
A string is a one dimensional array of characters
terminated by a null ‘0’ . You have learnt in 11
std a string can be defined and processed as,
char name [ ] = “POINTER”;
for ( I = 0 ; name [ i ] ! = ‘  0 ’ ; i + + )
cout<<name[i];
Alternatively the same can be achieved by
writing,
char name [ ] = “POINTER”;
char *cp;
for ( cp = name ; * cp ! = ‘  0 ’ ; cp + + )
cout<< *cp ;
POINTERS AND STRINGS
Another Example,
char *names [ ] = { “Sachin”, “Kapil”, “Ajay”, “Sunil”, “Anil” };
char *t;
t=name[1] ; //pointing to string “Kapil”
cout<<*t; // will produce out put “Kapil”
Similarly
T=name[3]; will point to?
POINTERS AND CONST
Constant pointer mean that the pointer in consideration will
always point to the same address . Its address ( to which it is
pointing to ) can not be modified.
For example,
int n=44;
int *const ptr = &n;
++(*ptr); // allowed since it modifies the content.
++ptr; // illegal because pointer ptr is constant pointer
CBSE QUESTION PAPER
QNO 1 (d) – 3 Marks
QNO 1 (e) – 3 Marks
1(d) What will be the output of the following program : 3
Delhi 2004
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
void ChangeString(char Text[], int &Counter)
{
char *Ptr = Text;
int Length = strlen (Text);
for ( ;Counter<Length-2; Counter+=2, Ptr++)
{
* (Ptr + Counter) = toupper( * (Ptr + Counter) );
}
}
void main()
{
clrscr();
int Position = 0;
char Messaget[] = “Pointers Fun”;
ChangeString (Message, Position);
cout<<Message<<“ @ “<<Position;
}
?Any Questions Please
Thank You
Very Much

More Related Content

What's hot

Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
Md. Afif Al Mamun
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
C pointer
C pointerC pointer
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
Wingston
 
ppt on pointers
ppt on pointersppt on pointers
ppt on pointers
Riddhi Patel
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
Ilio Catallo
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
Achyut Devkota
 
Pointers
PointersPointers
Pointers
sanya6900
 
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
Uri Dekel
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
Rumman Ansari
 
Pointers
PointersPointers
Pointers
sarith divakar
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer웅식 전
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
tanmaymodi4
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
minal kumar soni
 

What's hot (20)

Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointer
PointerPointer
Pointer
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
C pointer
C pointerC pointer
C pointer
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
ppt on pointers
ppt on pointersppt on pointers
ppt on pointers
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
 
Pointers
PointersPointers
Pointers
 
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
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Pointers
PointersPointers
Pointers
 
Pointers
PointersPointers
Pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 

Viewers also liked

Operating Systems - Intro to C++
Operating Systems - Intro to C++Operating Systems - Intro to C++
Operating Systems - Intro to C++
Emery Berger
 
Maharashtra hsc board previous year papers
Maharashtra hsc board  previous year papersMaharashtra hsc board  previous year papers
Maharashtra hsc board previous year papers
HSC.co.in
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
Saddam Hussain
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
Tobias Straub
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Laxman Puri
 
Career Option after 10+2
Career Option after 10+2Career Option after 10+2
Career Option after 10+2
vishalgarodia
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
geeortiz
 

Viewers also liked (10)

Operating Systems - Intro to C++
Operating Systems - Intro to C++Operating Systems - Intro to C++
Operating Systems - Intro to C++
 
Maharashtra hsc board previous year papers
Maharashtra hsc board  previous year papersMaharashtra hsc board  previous year papers
Maharashtra hsc board previous year papers
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Career Option after 10+2
Career Option after 10+2Career Option after 10+2
Career Option after 10+2
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 

Similar to 8 Pointers

Pointers
PointersPointers
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
Pointer
PointerPointer
Pointer
manish840
 
Pointers
PointersPointers
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
Sabaunnisa3
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
Sabi995708
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
ramya marichamy
 
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
Meghaj Mallick
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
WondimuBantihun1
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Vijayananda Ratnam Ch
 
Session 5
Session 5Session 5
Programming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explainedProgramming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explained
hozaifafadl
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
study material
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
HimanshuSharma997566
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
ssusere19c741
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
Vikram Nandini
 

Similar to 8 Pointers (20)

Pointers
PointersPointers
Pointers
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointer
PointerPointer
Pointer
 
Pointers
PointersPointers
Pointers
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
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
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Pointers
PointersPointers
Pointers
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Session 5
Session 5Session 5
Session 5
 
Programming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explainedProgramming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explained
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 

More from Praveen M Jigajinni

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
Praveen M Jigajinni
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
Praveen M Jigajinni
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
Praveen M Jigajinni
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
Praveen M Jigajinni
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
Praveen M Jigajinni
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
Praveen M Jigajinni
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
Praveen M Jigajinni
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
Praveen M Jigajinni
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
Praveen M Jigajinni
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
Praveen M Jigajinni
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
Praveen M Jigajinni
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
Praveen M Jigajinni
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
Praveen M Jigajinni
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
Praveen M Jigajinni
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
Praveen M Jigajinni
 

More from Praveen M Jigajinni (20)

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Chapter 13 exceptional handling
Chapter 13 exceptional handlingChapter 13 exceptional handling
Chapter 13 exceptional handling
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
 

Recently uploaded

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
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
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
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.
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
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
 
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
 
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
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
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
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
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
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
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
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
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
 
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
 
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
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
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
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 

8 Pointers

  • 1. WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT], MPhil (Comp. Sci), PGDCA, ADCA, Dc. Sc. & Engg.
  • 4. What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable. The Pointers are one of the C++’s most useful and powerful features.
  • 5. How Pointers are one of the C++’s most useful and powerful features? First : 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.
  • 6. Second: Pointer supports C++ dynamic allocation routines. Third: Pointer can improve the efficiency of certain routines. How Pointers are one of the C++’s most useful and powerful features?
  • 7. C++ MEMORY MAP STACK HEAP GLOBAL VARIABLES PROGRAM CODE STACK - This area is used for function calls return address, argument and local variables HEAP – This area is used for Dynamic Memory Allocation GLOBAL VARIABLES - This area is used to store global variables where they exists as long as the program continues
  • 8. 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
  • 9. 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.
  • 10. 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.
  • 11. 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.
  • 12. 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
  • 13. 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.
  • 14. Two Special Operators Two special operators are used * and & are with pointers. & operator is called as address of operator which provides the address of operand. * Operator is called as at address and also called as differencing which provides the value being pointed by pointer.
  • 15. Two Special Operators For example, int i = 25; int *iptr; iptr= &i; 25i 1050 1050iptr
  • 16. NULL Pointer (ZERO POINTER) A pointer variable must not remain uninitilized since uninitialized pointer cause system crash. Even if you do not have legal pointer value to initialize a pointer, you can initialize it with NULL pointer (ZERO POINTER). int *iptr=NULL; Note: In C++ version 11 nullptr keyword is introduced to assign null. nullptr is legal empty null pointer.
  • 17. 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--;
  • 18. 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.
  • 19. 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.
  • 20. 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
  • 21. 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
  • 22. 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.
  • 23. 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
  • 24. 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
  • 25. Improper use of new and delete may lead to memory leaks. Make sure that the memory allocated through new must be deleted through delete. Otherwise this may lead to adverse effect on the system DYNAMIC ALLOCATION OPERATORS – MEMORY LEAK
  • 26. It is faster to use an element pointer than an index when scanning arrays, For 1D Array, S[0]= *( S + 0 ) or *S or *(S) S[1] = *( S + 1 ) S[2] = *( S + 2 ) FOR 2D Arrays, S [ 0 ] [ 0 ] = *( S [ 0 ] + 0 ) or * ( * ( S + 0 ) ) S [ 0 ] [ 1 ] = *( S [ 0 ] + 1 ) or * ( * ( S + 1 ) ) S [ 1 ] [ 2 ] = *( S [ 1 ] + 2 ) or * ( * ( S + 1 ) + 2 ) ARRAYS
  • 27. POINTERS AND STRINGS A string is a one dimensional array of characters terminated by a null ‘0’ . You have learnt in 11 std a string can be defined and processed as, char name [ ] = “POINTER”; for ( I = 0 ; name [ i ] ! = ‘ 0 ’ ; i + + ) cout<<name[i]; Alternatively the same can be achieved by writing, char name [ ] = “POINTER”; char *cp; for ( cp = name ; * cp ! = ‘ 0 ’ ; cp + + ) cout<< *cp ;
  • 28. POINTERS AND STRINGS Another Example, char *names [ ] = { “Sachin”, “Kapil”, “Ajay”, “Sunil”, “Anil” }; char *t; t=name[1] ; //pointing to string “Kapil” cout<<*t; // will produce out put “Kapil” Similarly T=name[3]; will point to?
  • 29. POINTERS AND CONST Constant pointer mean that the pointer in consideration will always point to the same address . Its address ( to which it is pointing to ) can not be modified. For example, int n=44; int *const ptr = &n; ++(*ptr); // allowed since it modifies the content. ++ptr; // illegal because pointer ptr is constant pointer
  • 30. CBSE QUESTION PAPER QNO 1 (d) – 3 Marks QNO 1 (e) – 3 Marks
  • 31. 1(d) What will be the output of the following program : 3 Delhi 2004 #include<iostream.h> #include<ctype.h> #include<conio.h> #include<string.h> void ChangeString(char Text[], int &Counter) { char *Ptr = Text; int Length = strlen (Text); for ( ;Counter<Length-2; Counter+=2, Ptr++) { * (Ptr + Counter) = toupper( * (Ptr + Counter) ); } } void main() { clrscr(); int Position = 0; char Messaget[] = “Pointers Fun”; ChangeString (Message, Position); cout<<Message<<“ @ “<<Position; }