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

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

Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene1
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
Ponters
PontersPonters
Ponters
Anil Dutt
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ameer Hamxa
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
Krishna Nanda
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Monishkanungo
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
SANDIP MORADIYA
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
gourav kottawar
 
pointers
pointerspointers
pointers
teach4uin
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
ShashiShash2
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
Abubakar524802
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
SwapnaliPawar27
 

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

Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Ponters
PontersPonters
Ponters
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
pointers
pointerspointers
pointers
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
RashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
RashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
RashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 

Recently uploaded

AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
Prakhyath Rai
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
AnasAhmadNoor
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
vmspraneeth
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
uqyfuc
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Transcat
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
q30122000
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
Addu25809
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
sachin chaurasia
 

Recently uploaded (20)

AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
 

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; }