SlideShare a Scribd company logo
1
Structure
Object
Pointers
 Pointers are a special type of variables in which a
memory address is stored.
 They contain a memory address, not the value of the
variable.
 Pointers are an important and essential tool for
increasing the power of C++. A notable example is
the creation of data structures such as linked lists
and binary trees.
 In fact, several key features of C++, such as virtual
functions, the new operator, and the this pointer
require the use of pointers.
Address and Pointers
 The ideas behind pointers are not complicated. Here’s the
first key concept:
Every byte in the computer’s memory has an address.
 Addresses are numbers, just as they are for houses on a
street. The numbers start at 0 and go up from there 1, 2, 3,
and so on.
 If you have 1KB of memory, the highest address is 1023. (Of
course you have much more.)
 Your program, when it is loaded into memory, occupies a
certain range of these addresses.
 That means that every variable and every function in your
program starts at a Particular address. You can find the
address occupied by a variable by using the address-of
operator &.
The Address-of Operator &
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int var1 = 11; // define and
char var2 = 'A'; // initialize
float var3 = 33.34; // three variables
cout << "Address of Var1 = "
<< &var1 << endl;
cout <<"Address of Var2 = "
<< static_cast<void *>(&var2)
<< endl;
cout <<"Address of Var3 = "
<< &var3 << endl;
system("PAUSE");
return 0;
}
11
var1
0x6ffe1c
‘A’
0x6ffe1b
0x6ffe14
var3
var2
Pointer Variable
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int *ptr;
int var1 = 11;
int var2 = 22;
cout << "& ptr = " << &ptr << endl;
cout << "& var1 = " << &var1 << endl;
cout << "& var2 = " << &var2 << endl;
ptr = &var1; // pointer points to var1
cout <<" ptr = " << ptr << endl; // print pointer value
cout <<" *ptr = " << *ptr << endl; // print value of var1
ptr = &var2; // pointer points to var2
cout <<" ptr = " << ptr << endl; // print pointer value
cout <<" *ptr = " << *ptr << endl; // print value of var2
system("PAUSE");
return 0;
}
ptr
0x6ffe08
0x6ffe04
22
0x6ffe00
var2
var1
11
ptr
0x6ffe08
0x6ffe04
22
0x6ffe00
var2
var1
11
Pointer Variable
// other access using pointers
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int var1, var2; // two integer variables
int* ptr; // pointer to integers
ptr = &var1; // set pointer to address of var1
*ptr = 37; // same as var1 = 37
var2 = *ptr; // same as var2 = var1
cout << "var1 = " << var1 << endl; // verify var2 is 37
cout << "var2 = " << var2 << endl; // verify var2 is 37
system("PAUSE");
return 0;
}
var1
var2
ptr
37
37
Output
var1 = 37
var2 = 37
Press any key to continue...
Pointer to void
// pointers to type void
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int int_var; // integer variable
float flo_var; // float variable
int* ptr_int; // define pointer to int
float* ptr_flo; // define pointer to float
void* ptr_void; // define pointer to void
ptr_int = &int_var;// ok, int* to int*
// ptr_int = &flo_var; // error, float* to int*
// ptr_flo = &int_var; // error, int* to float*
ptr_flo = &flo_var; // ok, float* to float*
ptr_void = &int_var; // ok, int* to void*
ptr_void = &flo_var; // ok, float* to void*
system("PAUSE"); return 0; }
int_var
flo_var
ptr_int
ptr_flo
ptr_void
Pointers and Arrays
// array accessed with pointer notation
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int intarray[5] =
{ 31, 54, 77, 52, 93 };
for ( int j = 0 ; j < 5 ; j++ )
// print value
cout << *(intarray+j) << endl;
system("PAUSE");
return 0;
}
31
54
77
52
int_array
93
*(int_array
+
4)
*(int_array
+
3)
Pointer Arithmetic
// Adding and Subtracting a pointer variable
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int my_array[] =
{ 31, 54, 77, 52, 93 };
int* ptr;
ptr = my_array;
// print at index 0;
cout << *ptr << " ";
ptr = ptr + 2;
// print at index 2;
cout << *ptr << " ";
31 77
31
54
77
52
my_array
93
[0]
[1]
[2]
[3]
[4]
ptr_int
Pointer Arithmetic
ptr++;
// print at index 3;
cout << *ptr << " ";
ptr = ptr-2;
// print at index 1;
cout << *ptr << " ";
ptr--;
// print at index 0;
cout << *ptr << endl;
system("PAUSE");
return 0;
}
31 77
31
54
77
52
my_array
93
[0]
[1]
[2]
[3]
[4]
ptr_int
52 54 31
Press any key to ..
Pointer and Functions
// arguments passed by pointer
#include <iostream>
#include <stdlib.h>
using namespace std;
void centimize(double*);
int main()
{
double length = 10.0; // var has value of 10 inches
cout << "Length = " << length << " inches" << endl;
centimize(&length); // change var to centimeters
cout << "Length = " << length << " centimeters" << endl;
system("PAUSE");
return 0;
}
void centimize(double* ptr) // store the address in ptdr
{
*ptr = *ptr * 2.54; // *ptrd is the same as var
}
Pointer to String Constant
// strings defined using array and pointer notation
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
// str1 is an address, that is a pointer constant
char str1[] = "Defined as an array" ;
// while str2 is a pointer variable. it can be changed
char* str2 = "Defined as a pointer" ;
cout << str1 << endl; // display both strings
cout << str2 << endl;
// str1++; // can’t do this; str1 is a constant
str2++; // this is OK, str2 is a pointer
cout << str2 << endl; // now str2 starts “efined...”
system("PAUSE");
return 0;
}
str2
str1
D
e
f
i
n
e
d
a
s
a
n
a
r
r
a
y
0
D
e
f
i
n
e
d
a
s
a
p
o
i
n
t
e
r
0
String as Function Argument
// displays a string with pointer notation
#include <iostream>
#include <stdlib.h>
using namespace std;
void dispstr(char*); // prototype
int main()
{
char str[] = "This is amazing" ;
dispstr(str); // display the string
system("PAUSE"); return 0;
}
void dispstr(char* ps){
while( *ps ) // until null ('0') character,
cout << *ps++; // print characters
cout << endl;
}
ps
T
h
i
s
i
s
a
m
a
z
i
n
g
0
str
Copying a String Using Pointers
// copies one string to another with pointers
#include <iostream>
#include <stdlib.h>
using namespace std;
void copystr(char*, const char*); // function declaration
int main(){
char* str1 = "Self-conquest is the greatest victory." ;
char str2[80]; // empty string
copystr(str2, str1); // copy str1 to str2
cout << str2 << endl; // display str2
system("PAUSE"); return 0;
}
void copystr(char* dest, const char* src){
while( *src ){ // until null ('0') character
*dest = *src ; // copy chars from src to dest
dest++; src++ ; // increment pointers
} *dest = '0';} // copy null character
The const Modifier and Pointers
 The use of the const modifier with pointer declarations can be
confusing, because it can mean one of two things, depending on
where it’s placed. The following statements show the two
possibilities:
const int* cptrInt; // cptrInt is a pointer to constant int
int* const ptrcInt; // ptrcInt is a constant pointer to int
 In the first declaration, you cannot change the value of whatever
cptrInt points to, although you can change cptrInt itself.
 In the second declaration, you can change what ptrcInt points to,
but you cannot change the value of ptrcInt itself.
 You can remember the difference by reading from right to left, as
indicated in the comments.
The const Modifier and Pointers
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
const int a = 5; int b = 10;
const int* cptrInt; // cptrInt is a pointer to constant int
// ptrcInt is a constant pointer to int
int* const ptrcInt = &b;
cptrInt = &a;
// *cptrInt = 25; // can not change a constant value
*ptrcInt = 100;
cptrInt = &b;
// ptrcInt = &a; // can not change address of pointer
cout << "*ptrcInt = " << *ptrcInt << endl;
cout << "*cptrInt = " << *cptrInt << endl;
system("PAUSE"); return 0; }

More Related Content

PDF
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
PDF
PSPC--UNIT-5.pdf
ODP
Pointers in c++ by minal
PPTX
Pointer in C
PPTX
Arrays to arrays and pointers with arrays.pptx
PPTX
C++ Pointer | Introduction to programming
PPTX
Pointers in C
PPTX
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
PSPC--UNIT-5.pdf
Pointers in c++ by minal
Pointer in C
Arrays to arrays and pointers with arrays.pptx
C++ Pointer | Introduction to programming
Pointers in C
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

Similar to Object Oriented Programming using C++: Ch10 Pointers.pptx (20)

PPTX
pointers.pptx
PPTX
Unit-I Pointer Data structure.pptx
PPTX
Computer Programming Lecture numer 05 -- pointers,variablesb
PDF
VIT351 Software Development VI Unit3
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
PPTX
Pointers in C++ object oriented programming
PDF
Computer programming course(c++ pointer))
PDF
Chapter 5 (Part I) - Pointers.pdf
PPTX
Introduction to pointers in c plus plus .
PPT
Unit 6 pointers
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
PPTX
COM1407: Working with Pointers
PPT
FP 201 - Unit 6
PPTX
Ponters
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PDF
Lk module5 pointers
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Pointers in c++
PDF
Pointers in C
pointers.pptx
Unit-I Pointer Data structure.pptx
Computer Programming Lecture numer 05 -- pointers,variablesb
VIT351 Software Development VI Unit3
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
Pointers in C++ object oriented programming
Computer programming course(c++ pointer))
Chapter 5 (Part I) - Pointers.pdf
Introduction to pointers in c plus plus .
Unit 6 pointers
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
COM1407: Working with Pointers
FP 201 - Unit 6
Ponters
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Lk module5 pointers
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
Pointers in c++
Pointers in C
Ad

More from RashidFaridChishti (20)

PPTX
DBMS: Week 15 - Database Security and Access Control
PPTX
DBMS: Week 14 - Backup and Recovery in MySQL
PPTX
DBMS: Week 13 - Transactions and Concurrency Control
PPTX
DBMS: Week 12 - Triggers in MySQL Database Server
PPTX
DBMS: Week 11 - Stored Procedures and Functions
PPTX
DBMS: Week 10 - Database Design and Normalization
PPTX
DBMS: Week 09 - SQL Constraints and Indexing
PPTX
DBMS: Week 08 - Joins and Views in MySQL
PPTX
DBMS: Week 07 - Advanced SQL Queries in MySQL
PPTX
DBMS: Week 06 - SQL - Data Manipulation Language (DML)
PPTX
DBMS: Week 05 - Introduction to SQL Query
PPTX
DBMS: Week 04 - Relational Model in a Database
PPTX
DBMS: Week 03 - Data Models and ER Model
PPTX
DBMS: Week 02 - Database System Architecture
PPTX
DBMS: Week 01 - Introduction to Databases
DOCX
Lab Manual Arduino UNO Microcontrollar.docx
DOCX
Object Oriented Programming OOP Lab Manual.docx
DOCX
Lab Manual Data Structure and Algorithm.docx
PPTX
Data Structures and Agorithm: DS 24 Hash Tables.pptx
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
DBMS: Week 15 - Database Security and Access Control
DBMS: Week 14 - Backup and Recovery in MySQL
DBMS: Week 13 - Transactions and Concurrency Control
DBMS: Week 12 - Triggers in MySQL Database Server
DBMS: Week 11 - Stored Procedures and Functions
DBMS: Week 10 - Database Design and Normalization
DBMS: Week 09 - SQL Constraints and Indexing
DBMS: Week 08 - Joins and Views in MySQL
DBMS: Week 07 - Advanced SQL Queries in MySQL
DBMS: Week 06 - SQL - Data Manipulation Language (DML)
DBMS: Week 05 - Introduction to SQL Query
DBMS: Week 04 - Relational Model in a Database
DBMS: Week 03 - Data Models and ER Model
DBMS: Week 02 - Database System Architecture
DBMS: Week 01 - Introduction to Databases
Lab Manual Arduino UNO Microcontrollar.docx
Object Oriented Programming OOP Lab Manual.docx
Lab Manual Data Structure and Algorithm.docx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Ad

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
web development for engineering and engineering
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
CYBER-CRIMES AND SECURITY A guide to understanding
Geodesy 1.pptx...............................................
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Digital Logic Computer Design lecture notes
UNIT 4 Total Quality Management .pptx
web development for engineering and engineering
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Foundation to blockchain - A guide to Blockchain Tech
Lesson 3_Tessellation.pptx finite Mathematics
Arduino robotics embedded978-1-4302-3184-4.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems

Object Oriented Programming using C++: Ch10 Pointers.pptx

  • 2. Pointers  Pointers are a special type of variables in which a memory address is stored.  They contain a memory address, not the value of the variable.  Pointers are an important and essential tool for increasing the power of C++. A notable example is the creation of data structures such as linked lists and binary trees.  In fact, several key features of C++, such as virtual functions, the new operator, and the this pointer require the use of pointers.
  • 3. Address and Pointers  The ideas behind pointers are not complicated. Here’s the first key concept: Every byte in the computer’s memory has an address.  Addresses are numbers, just as they are for houses on a street. The numbers start at 0 and go up from there 1, 2, 3, and so on.  If you have 1KB of memory, the highest address is 1023. (Of course you have much more.)  Your program, when it is loaded into memory, occupies a certain range of these addresses.  That means that every variable and every function in your program starts at a Particular address. You can find the address occupied by a variable by using the address-of operator &.
  • 4. The Address-of Operator & #include <iostream> #include <stdlib.h> using namespace std; int main(){ int var1 = 11; // define and char var2 = 'A'; // initialize float var3 = 33.34; // three variables cout << "Address of Var1 = " << &var1 << endl; cout <<"Address of Var2 = " << static_cast<void *>(&var2) << endl; cout <<"Address of Var3 = " << &var3 << endl; system("PAUSE"); return 0; } 11 var1 0x6ffe1c ‘A’ 0x6ffe1b 0x6ffe14 var3 var2
  • 5. Pointer Variable #include <iostream> #include <stdlib.h> using namespace std; int main() { int *ptr; int var1 = 11; int var2 = 22; cout << "& ptr = " << &ptr << endl; cout << "& var1 = " << &var1 << endl; cout << "& var2 = " << &var2 << endl; ptr = &var1; // pointer points to var1 cout <<" ptr = " << ptr << endl; // print pointer value cout <<" *ptr = " << *ptr << endl; // print value of var1 ptr = &var2; // pointer points to var2 cout <<" ptr = " << ptr << endl; // print pointer value cout <<" *ptr = " << *ptr << endl; // print value of var2 system("PAUSE"); return 0; }
  • 7. Pointer Variable // other access using pointers #include <iostream> #include <stdlib.h> using namespace std; int main() { int var1, var2; // two integer variables int* ptr; // pointer to integers ptr = &var1; // set pointer to address of var1 *ptr = 37; // same as var1 = 37 var2 = *ptr; // same as var2 = var1 cout << "var1 = " << var1 << endl; // verify var2 is 37 cout << "var2 = " << var2 << endl; // verify var2 is 37 system("PAUSE"); return 0; } var1 var2 ptr 37 37 Output var1 = 37 var2 = 37 Press any key to continue...
  • 8. Pointer to void // pointers to type void #include <iostream> #include <stdlib.h> using namespace std; int main(){ int int_var; // integer variable float flo_var; // float variable int* ptr_int; // define pointer to int float* ptr_flo; // define pointer to float void* ptr_void; // define pointer to void ptr_int = &int_var;// ok, int* to int* // ptr_int = &flo_var; // error, float* to int* // ptr_flo = &int_var; // error, int* to float* ptr_flo = &flo_var; // ok, float* to float* ptr_void = &int_var; // ok, int* to void* ptr_void = &flo_var; // ok, float* to void* system("PAUSE"); return 0; } int_var flo_var ptr_int ptr_flo ptr_void
  • 9. Pointers and Arrays // array accessed with pointer notation #include <iostream> #include <stdlib.h> using namespace std; int main() { int intarray[5] = { 31, 54, 77, 52, 93 }; for ( int j = 0 ; j < 5 ; j++ ) // print value cout << *(intarray+j) << endl; system("PAUSE"); return 0; } 31 54 77 52 int_array 93 *(int_array + 4) *(int_array + 3)
  • 10. Pointer Arithmetic // Adding and Subtracting a pointer variable #include <iostream> #include <stdlib.h> using namespace std; int main(){ int my_array[] = { 31, 54, 77, 52, 93 }; int* ptr; ptr = my_array; // print at index 0; cout << *ptr << " "; ptr = ptr + 2; // print at index 2; cout << *ptr << " "; 31 77 31 54 77 52 my_array 93 [0] [1] [2] [3] [4] ptr_int
  • 11. Pointer Arithmetic ptr++; // print at index 3; cout << *ptr << " "; ptr = ptr-2; // print at index 1; cout << *ptr << " "; ptr--; // print at index 0; cout << *ptr << endl; system("PAUSE"); return 0; } 31 77 31 54 77 52 my_array 93 [0] [1] [2] [3] [4] ptr_int 52 54 31 Press any key to ..
  • 12. Pointer and Functions // arguments passed by pointer #include <iostream> #include <stdlib.h> using namespace std; void centimize(double*); int main() { double length = 10.0; // var has value of 10 inches cout << "Length = " << length << " inches" << endl; centimize(&length); // change var to centimeters cout << "Length = " << length << " centimeters" << endl; system("PAUSE"); return 0; } void centimize(double* ptr) // store the address in ptdr { *ptr = *ptr * 2.54; // *ptrd is the same as var }
  • 13. Pointer to String Constant // strings defined using array and pointer notation #include <iostream> #include <stdlib.h> using namespace std; int main(){ // str1 is an address, that is a pointer constant char str1[] = "Defined as an array" ; // while str2 is a pointer variable. it can be changed char* str2 = "Defined as a pointer" ; cout << str1 << endl; // display both strings cout << str2 << endl; // str1++; // can’t do this; str1 is a constant str2++; // this is OK, str2 is a pointer cout << str2 << endl; // now str2 starts “efined...” system("PAUSE"); return 0; } str2 str1 D e f i n e d a s a n a r r a y 0 D e f i n e d a s a p o i n t e r 0
  • 14. String as Function Argument // displays a string with pointer notation #include <iostream> #include <stdlib.h> using namespace std; void dispstr(char*); // prototype int main() { char str[] = "This is amazing" ; dispstr(str); // display the string system("PAUSE"); return 0; } void dispstr(char* ps){ while( *ps ) // until null ('0') character, cout << *ps++; // print characters cout << endl; } ps T h i s i s a m a z i n g 0 str
  • 15. Copying a String Using Pointers // copies one string to another with pointers #include <iostream> #include <stdlib.h> using namespace std; void copystr(char*, const char*); // function declaration int main(){ char* str1 = "Self-conquest is the greatest victory." ; char str2[80]; // empty string copystr(str2, str1); // copy str1 to str2 cout << str2 << endl; // display str2 system("PAUSE"); return 0; } void copystr(char* dest, const char* src){ while( *src ){ // until null ('0') character *dest = *src ; // copy chars from src to dest dest++; src++ ; // increment pointers } *dest = '0';} // copy null character
  • 16. The const Modifier and Pointers  The use of the const modifier with pointer declarations can be confusing, because it can mean one of two things, depending on where it’s placed. The following statements show the two possibilities: const int* cptrInt; // cptrInt is a pointer to constant int int* const ptrcInt; // ptrcInt is a constant pointer to int  In the first declaration, you cannot change the value of whatever cptrInt points to, although you can change cptrInt itself.  In the second declaration, you can change what ptrcInt points to, but you cannot change the value of ptrcInt itself.  You can remember the difference by reading from right to left, as indicated in the comments.
  • 17. The const Modifier and Pointers #include <iostream> #include <stdlib.h> using namespace std; int main(){ const int a = 5; int b = 10; const int* cptrInt; // cptrInt is a pointer to constant int // ptrcInt is a constant pointer to int int* const ptrcInt = &b; cptrInt = &a; // *cptrInt = 25; // can not change a constant value *ptrcInt = 100; cptrInt = &b; // ptrcInt = &a; // can not change address of pointer cout << "*ptrcInt = " << *ptrcInt << endl; cout << "*cptrInt = " << *cptrInt << endl; system("PAUSE"); return 0; }