SlideShare a Scribd company logo
1 of 18
POINTER
BY
RISHIVINTHIYA
M.Sc(IT)
Pointers
 A pointer is a variable used to store the
address of a memory cell.
 We can use the pointer to reference this
memory cell
100… … 1024 …
Memory address: 1024 1032
…
1020
integer
pointer
Pointer Types
 Pointer
 C++ has pointer types for each type of
object
○ Pointers to int objects
○ Pointers to char objects
○ Pointers to user-defined objects
(e.g., RationalNumber)
 Even pointers to pointers
○ Pointers to pointers to int objects
Pointer Variable
 Declaration of Pointer variables
type* pointer_name;
//or
type *pointer_name;
where type is the type of data pointed to (e.g. int, char,
double)
Examples:
int *n;
RationalNumber *r;
int **p; // pointer to pointer
Address Operator &
 The "address of " operator (&) gives the memory
address of the variable
 Usage: &variable_name
100… … … …
Memory address: 1024
int a = 100;
//get the value,
cout << a; //prints 100
//get the memory address
cout << &a; //prints 1024
…
1020
a
Address Operator &
10088 … … …
Memory address: 1024 1032
a
…
1020
b
#include <iostream>
using namespace std;
void main(){
int a, b;
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
 Result is:
The address of a is: 1020
The address of b is: 1024
Pointer Variables
 The value of pointer p is the address of variable a
 A pointer is also a variable, so it has its own memory
address
10088 … 1024 …
Memory address: 1024 1032
…
1020
a p
int a = 100;
int *p = &a;
cout << a << " " << &a <<endl;
cout << p << " " << &p <<endl;
 Result is:
100 1024
1024 1032
Pointer to Pointer
Dereferencing Operator *
 We can access to the value stored in the variable
pointed to by using the dereferencing operator (*),
10088 … 1024 …
Memory address: 1024 1032
…
1020
int a = 100;
int *p = &a;
cout << a << endl;
cout << &a << endl;
cout << p << " " << *p << endl;
cout << &p << endl;
 Result is:
100
1024
1024 100
1032
a p
Another Pointer Example
int a = 3;
char s = ‘z’;
double d = 1.03;
int *pa = &a;
char *ps = &s;
double *pd = &d;
% sizeof returns the # of bytes…
cout << sizeof(pa) << sizeof(*pa)
<< sizeof(&pa) << endl;
cout << sizeof(ps) << sizeof(*ps)
<< sizeof(&ps) << endl;
cout << sizeof(pd) << sizeof(*pd)
<< sizeof(&pd) << endl;
Reference Variables
 A reference variable serves as an alternative
name for an object
int m = 10;
int &j = m; // j is a reference
variable
cout << “value of m = “ << m << endl;
//print 10
j = 18;
cout << “value of m = “ << m << endl;
// print 18
Pointers and Arrays
 The name of an array points only to the first
element not the whole array.
1000
1012
1016
1004
1008
Array Name is a pointer constant
#include <iostream>
using namespace std;
void main (){
int a[5];
cout << "Address of a[0]: " << &a[0] << endl
<< "Name as pointer: " << a << endl;
}
Result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4
Dereferencing An Array Name
#include <iostream>
using namespace std;
void main(){
int a[5] = {2,4,6,8,22};
cout << *a << " "
<< a[0];
} //main
2
4
8
6
22a[4]
a[0]
a[2]
a[1]
a[3]
Pointer Arithmetic
 Given a pointer p, p+n refers to the element that
is offset from p by n positions.
2
4
8
6
22
a
a + 2
a + 4
a + 3
a + 1 p
p + 2
p + 3
p - 1
p + 1
*(a+n) is identical to a[n]
Dereferencing Array Pointers
2
4
8
6
22
a
a + 2
a + 4
a + 3
a + 1
a[3] or *(a + 3)
a[2] or *(a + 2)
a[1] or *(a + 1)
a[0] or *(a + 0)
a[4] or *(a + 4)
Array of Pointers & Pointers to Array
a
b
c
An array of Pointers
p
int a = 1, b = 2, c = 3;
int *p[5];
p[0] = &a;
p[1] = &b;
p[2] = &c;
int list[5] = {9, 8, 7, 6, 5};
int *p;
P = list;//points to 1st entry
P = &list[0];//points to 1st entry
P = &list[1];//points to 2nd entry
P = list + 1; //points to 2nd entry
A pointer to an array
NULL pointer
 NULL is a special value that indicates an empty pointer
 If you try to access a NULL pointer, you will get an error
int *p;
p = 0;
cout << p << endl; //prints 0
cout << &p << endl;//prints address of p
cout << *p << endl;//Error!

More Related Content

What's hot

Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

What's hot (20)

Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
File in C language
File in C languageFile in C language
File in C language
 
Data types in java
Data types in javaData types in java
Data types in java
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 

Similar to Pointers

Similar to Pointers (20)

Pointer
PointerPointer
Pointer
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Pointers
PointersPointers
Pointers
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Pointers
PointersPointers
Pointers
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
C pointers
C pointersC pointers
C pointers
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Unit 3
Unit 3Unit 3
Unit 3
 
pointers
pointerspointers
pointers
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
CPointer-hk.ppt
CPointer-hk.pptCPointer-hk.ppt
CPointer-hk.ppt
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
 

More from rajshreemuthiah (20)

oracle
oracleoracle
oracle
 
quality
qualityquality
quality
 
bigdata
bigdatabigdata
bigdata
 
polymorphism
polymorphismpolymorphism
polymorphism
 
solutions and understanding text analytics
solutions and understanding text analyticssolutions and understanding text analytics
solutions and understanding text analytics
 
interface
interfaceinterface
interface
 
Testing &ampdebugging
Testing &ampdebuggingTesting &ampdebugging
Testing &ampdebugging
 
concurrency control
concurrency controlconcurrency control
concurrency control
 
Education
EducationEducation
Education
 
Formal verification
Formal verificationFormal verification
Formal verification
 
Transaction management
Transaction management Transaction management
Transaction management
 
Multi thread
Multi threadMulti thread
Multi thread
 
System testing
System testingSystem testing
System testing
 
software maintenance
software maintenancesoftware maintenance
software maintenance
 
exception handling
exception handlingexception handling
exception handling
 
e governance
e governancee governance
e governance
 
recovery management
recovery managementrecovery management
recovery management
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
 
Buffer managements
Buffer managementsBuffer managements
Buffer managements
 
os linux
os linuxos linux
os linux
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

Pointers

  • 2. Pointers  A pointer is a variable used to store the address of a memory cell.  We can use the pointer to reference this memory cell 100… … 1024 … Memory address: 1024 1032 … 1020 integer pointer
  • 3. Pointer Types  Pointer  C++ has pointer types for each type of object ○ Pointers to int objects ○ Pointers to char objects ○ Pointers to user-defined objects (e.g., RationalNumber)  Even pointers to pointers ○ Pointers to pointers to int objects
  • 4. Pointer Variable  Declaration of Pointer variables type* pointer_name; //or type *pointer_name; where type is the type of data pointed to (e.g. int, char, double) Examples: int *n; RationalNumber *r; int **p; // pointer to pointer
  • 5. Address Operator &  The "address of " operator (&) gives the memory address of the variable  Usage: &variable_name 100… … … … Memory address: 1024 int a = 100; //get the value, cout << a; //prints 100 //get the memory address cout << &a; //prints 1024 … 1020 a
  • 6. Address Operator & 10088 … … … Memory address: 1024 1032 a … 1020 b #include <iostream> using namespace std; void main(){ int a, b; a = 88; b = 100; cout << "The address of a is: " << &a << endl; cout << "The address of b is: " << &b << endl; }  Result is: The address of a is: 1020 The address of b is: 1024
  • 7. Pointer Variables  The value of pointer p is the address of variable a  A pointer is also a variable, so it has its own memory address 10088 … 1024 … Memory address: 1024 1032 … 1020 a p int a = 100; int *p = &a; cout << a << " " << &a <<endl; cout << p << " " << &p <<endl;  Result is: 100 1024 1024 1032
  • 9. Dereferencing Operator *  We can access to the value stored in the variable pointed to by using the dereferencing operator (*), 10088 … 1024 … Memory address: 1024 1032 … 1020 int a = 100; int *p = &a; cout << a << endl; cout << &a << endl; cout << p << " " << *p << endl; cout << &p << endl;  Result is: 100 1024 1024 100 1032 a p
  • 10. Another Pointer Example int a = 3; char s = ‘z’; double d = 1.03; int *pa = &a; char *ps = &s; double *pd = &d; % sizeof returns the # of bytes… cout << sizeof(pa) << sizeof(*pa) << sizeof(&pa) << endl; cout << sizeof(ps) << sizeof(*ps) << sizeof(&ps) << endl; cout << sizeof(pd) << sizeof(*pd) << sizeof(&pd) << endl;
  • 11. Reference Variables  A reference variable serves as an alternative name for an object int m = 10; int &j = m; // j is a reference variable cout << “value of m = “ << m << endl; //print 10 j = 18; cout << “value of m = “ << m << endl; // print 18
  • 12. Pointers and Arrays  The name of an array points only to the first element not the whole array. 1000 1012 1016 1004 1008
  • 13. Array Name is a pointer constant #include <iostream> using namespace std; void main (){ int a[5]; cout << "Address of a[0]: " << &a[0] << endl << "Name as pointer: " << a << endl; } Result: Address of a[0]: 0x0065FDE4 Name as pointer: 0x0065FDE4
  • 14. Dereferencing An Array Name #include <iostream> using namespace std; void main(){ int a[5] = {2,4,6,8,22}; cout << *a << " " << a[0]; } //main 2 4 8 6 22a[4] a[0] a[2] a[1] a[3]
  • 15. Pointer Arithmetic  Given a pointer p, p+n refers to the element that is offset from p by n positions. 2 4 8 6 22 a a + 2 a + 4 a + 3 a + 1 p p + 2 p + 3 p - 1 p + 1
  • 16. *(a+n) is identical to a[n] Dereferencing Array Pointers 2 4 8 6 22 a a + 2 a + 4 a + 3 a + 1 a[3] or *(a + 3) a[2] or *(a + 2) a[1] or *(a + 1) a[0] or *(a + 0) a[4] or *(a + 4)
  • 17. Array of Pointers & Pointers to Array a b c An array of Pointers p int a = 1, b = 2, c = 3; int *p[5]; p[0] = &a; p[1] = &b; p[2] = &c; int list[5] = {9, 8, 7, 6, 5}; int *p; P = list;//points to 1st entry P = &list[0];//points to 1st entry P = &list[1];//points to 2nd entry P = list + 1; //points to 2nd entry A pointer to an array
  • 18. NULL pointer  NULL is a special value that indicates an empty pointer  If you try to access a NULL pointer, you will get an error int *p; p = 0; cout << p << endl; //prints 0 cout << &p << endl;//prints address of p cout << *p << endl;//Error!