SlideShare a Scribd company logo
2
Most read
7
Most read
15
Most read
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

PPT
Bubble sort
PDF
Python exception handling
PPTX
PPTX
The string class
PDF
Python programming : Standard Input and Output
PDF
Python basic
PPT
Strings
PPTX
class and objects
Bubble sort
Python exception handling
The string class
Python programming : Standard Input and Output
Python basic
Strings
class and objects

What's hot (20)

PDF
Introduction to XHTML
PPTX
Pointers in c language
PPTX
List in Python
PPTX
Unary operator overloading
PDF
Python tuple
PPTX
Inheritance in c++
PPTX
Basics of Object Oriented Programming in Python
PPTX
PPT
Function overloading(c++)
PPSX
python Function
PPTX
Javascript alert and confrim box
PPTX
Inline function
PDF
Bca sem 6 php practicals 1to12
PDF
C Pointers
PPTX
File Management in C
PPTX
Types of function call
PPTX
C++ programming function
PPTX
Presentation on pointer.
PPT
C operator and expression
Introduction to XHTML
Pointers in c language
List in Python
Unary operator overloading
Python tuple
Inheritance in c++
Basics of Object Oriented Programming in Python
Function overloading(c++)
python Function
Javascript alert and confrim box
Inline function
Bca sem 6 php practicals 1to12
C Pointers
File Management in C
Types of function call
C++ programming function
Presentation on pointer.
C operator and expression
Ad

Similar to Pointers (20)

PPT
Pointer
PPT
Lect 7 - 8 Pointers.pptffffffdfgfffddddg
ODP
Pointers in c++ by minal
PPT
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
PPT
Pointers
PPT
Pointers_in_c.pptfffgfggdggffffrreeeggttr
PPT
C/C++ Pointers explanationwith different examples
PPTX
Computer Programming Lecture numer 05 -- pointers,variablesb
PPTX
INTRODUCTION TO POINTER IN c++ AND POLYMORPHISM
PPTX
Object Oriented Programming using C++: Ch10 Pointers.pptx
PPTX
Chp3(pointers ref)
PPTX
Pointers in c++
PDF
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
PPTX
pointers.pptx
PDF
C++ computer language chapter 4 pointers.pdf
PPTX
ppt on pointers
PPT
Data structure and problem solving ch05.ppt
PPTX
Pf cs102 programming-9 [pointers]
PPT
Lecture2.ppt
PDF
Chapter 5 (Part I) - Pointers.pdf
Pointer
Lect 7 - 8 Pointers.pptffffffdfgfffddddg
Pointers in c++ by minal
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
Pointers
Pointers_in_c.pptfffgfggdggffffrreeeggttr
C/C++ Pointers explanationwith different examples
Computer Programming Lecture numer 05 -- pointers,variablesb
INTRODUCTION TO POINTER IN c++ AND POLYMORPHISM
Object Oriented Programming using C++: Ch10 Pointers.pptx
Chp3(pointers ref)
Pointers in c++
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
pointers.pptx
C++ computer language chapter 4 pointers.pdf
ppt on pointers
Data structure and problem solving ch05.ppt
Pf cs102 programming-9 [pointers]
Lecture2.ppt
Chapter 5 (Part I) - Pointers.pdf
Ad

More from rajshreemuthiah (20)

PPTX
PPTX
PPTX
PPTX
polymorphism
PPTX
solutions and understanding text analytics
PPTX
interface
PPTX
Testing &ampdebugging
PPTX
concurrency control
PPTX
Education
PPTX
Formal verification
PPTX
Transaction management
PPTX
Multi thread
PPTX
System testing
PPTX
software maintenance
PPTX
exception handling
PPTX
e governance
PPTX
recovery management
PPTX
Implementing polymorphism
PPSX
Buffer managements
PPTX
os linux
polymorphism
solutions and understanding text analytics
interface
Testing &ampdebugging
concurrency control
Education
Formal verification
Transaction management
Multi thread
System testing
software maintenance
exception handling
e governance
recovery management
Implementing polymorphism
Buffer managements
os linux

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation theory and applications.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Advanced methodologies resolving dimensionality complications for autism neur...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Mobile App Security Testing_ A Comprehensive Guide.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf

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!