SlideShare a Scribd company logo
Pointers
Pointers and dynamic objects/ Slide 2
Topics
 Pointers
 Memory addresses
 Declaration
 Dereferencing a pointer
 Pointers to pointer
Pointers and dynamic objects/ Slide 3
Computer Memory
 Each variable is assigned a memory slot (the
size depends on the data type) and the
variable’s data is stored there
Variable a’s value, i.e., 100, is
stored at memory location 1024
100
100
…
… …
… 1024
1024 …
…
emory address: 1024 1032
int a = 100;
…
…
1020
a
Pointers and dynamic objects/ Slide 4
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
100
…
… …
… 1024
1024 …
…
Memory address: 1024 1032
…
…
1020
integer pointer
Pointers and dynamic objects/ Slide 5
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
Pointers and dynamic objects/ Slide 6
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;
int **p; // pointer to pointer
Pointers and dynamic objects/ Slide 7
Address Operator &
 The "address of " operator (&) gives the memory
address of the variable
 Usage: &variable_name
100
100
…
… …
… …
… …
…
Memory address: 1024
int a = 100;
//get the value,
cout << a; //prints 100
//get the memory address
cout << &a; //prints 1024
…
…
1020
a
Pointers and dynamic objects/ Slide 8
Address Operator &
100
100
88
88 …
… …
… …
…
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
Pointers and dynamic objects/ Slide 9
 The value of pointer p is the address of variable a
 A pointer is also a variable, so it has its own memory address
Pointer Variables
100
100
88
88 …
… 1024
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
Pointers and dynamic objects/ Slide 10
Pointer to Pointer
What is the output?
58 58 58
Pointers and dynamic objects/ Slide 11
Dereferencing Operator *
 We can access to the value stored in the variable
pointed to by using the dereferencing operator (*),
100
100
88
88 …
… 1024
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
Pointers and dynamic objects/ Slide 12
Reference Variables
 A reference variable always refers to the
same object. Assigning a reference variable
with a new value actually changes the value
of the referred object.
 Reference variables are commonly used for
parameter passing to a function
Pointers and dynamic objects/ Slide 13
Pass by Reference
void Swap(int &y, int &z) {
int temp = y;
y = z;
z = temp;
}
int main() {
int a = 99;
int b = 55;
Swap(a, b);
cout << a << b << endl;
return 0;
}
Pointers and dynamic objects/ Slide 14
Usage Pointer
void Swap(int *Ptr1, int *Ptr2){
int temp = *Ptr1;
*Ptr1 = *Ptr2;
*Ptr2 = temp;
}
int main() {
int a = 99;
int b = 55;
Swap(&a, &b);
cout << a << b << endl;
return 0;
}
Pointers and dynamic objects/ Slide 15
Another Example
The code
void doubleIt(int x,
int * p)
{
*p = 2 * x;
}
int main()
{
int a = 16;
doubleIt(9, &a);
return 0;
}
Box diagram
Memory Layout
9
x
p
(8200)
x
(8196)
16
a
main
doubleIt
p
a
(8192)
16
9
8192
main
doubleIt
a gets 18
Pointers and dynamic objects/ Slide 16
Pointers and Arrays
 The name of an array points only to the first
element not the whole array.
1000
1012
1016
1004
1008
Pointers and dynamic objects/ Slide 17
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
Pointers and dynamic objects/ Slide 18
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
22
a[4]
a[0]
a[2]
a[1]
a[3]
a
a
This element is
called a[0] or *a
Pointers and dynamic objects/ Slide 19
Array Names as Pointers
 To access an array, any pointer to the first element
can be used instead of the name of the array.
We could replace *p by *a
2 2
#include <iostream>
using namespace std;
void main(){
int a[5] = {2,4,6,8,22};
int *p = a;
cout << a[0] << " "
<< *p;
}
2
4
8
6
22
a[4]
a[0]
a[2]
a[1]
a[3]
a p
a
Pointers and dynamic objects/ Slide 20
Multiple Array Pointers
 Both a and p are pointers to the same array.
2 2
4 4
#include <iostream>
using namespace std;
void main(){
int a[5] = {2,4,6,8,22};
int *p = &a[1];
cout << a[0] << " "
<< p[-1];
cout << a[1] << " "
<< p[0];
}
2
4
8
6
22
a[4]
a[0]
a[2]
a[1]
a[3]
p
p[0]
a[0]
Pointers and dynamic objects/ Slide 21
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
Pointers and dynamic objects/ Slide 22
*(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)
 Note: flexible pointer syntax
Pointers and dynamic objects/ Slide 23
Examples
…
Pointers and dynamic objects/ Slide 24
End

More Related Content

PPT
C/C++ Pointers explanationwith different examples
PPT
Pointers_in_c.pptfffgfggdggffffrreeeggttr
PPT
Pointers
PPT
Pointers
PPT
Pointer
PPT
Pointers
ODP
Pointers in c++ by minal
PPTX
Engineering Computers L32-L33-Pointers.pptx
C/C++ Pointers explanationwith different examples
Pointers_in_c.pptfffgfggdggffffrreeeggttr
Pointers
Pointers
Pointer
Pointers
Pointers in c++ by minal
Engineering Computers L32-L33-Pointers.pptx

Similar to Lect 7 - 8 Pointers.pptffffffdfgfffddddg (20)

PPT
Data structure and problem solving ch05.ppt
PDF
Chapter 5 (Part I) - Pointers.pdf
PPTX
Algoritmos e Estruturas de Dados - Pointers
PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
PPT
l7-pointers.ppt
PPTX
CSE 1102 - Lecture_8 - Pointers_in_C.pptx
PPT
pointers
PDF
Pointers in Programming
PPTX
Pointer in C++
PPTX
Understanding the concept of Pointers.pptx
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PDF
Object Oriented Programming using C++ - Part 4
PDF
polymorphism in c++ with Full Explanation.
PPTX
Programming Fundamentals
PPT
Unit 6 pointers
PPTX
Pointers
PPTX
INTRODUCTION TO POINTER IN c++ AND POLYMORPHISM
PDF
C Programming - Refresher - Part III
PPTX
week14Pointers_II. pointers pemrograman dasar C++.pptx
PPT
FP 201 - Unit 6
Data structure and problem solving ch05.ppt
Chapter 5 (Part I) - Pointers.pdf
Algoritmos e Estruturas de Dados - Pointers
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
l7-pointers.ppt
CSE 1102 - Lecture_8 - Pointers_in_C.pptx
pointers
Pointers in Programming
Pointer in C++
Understanding the concept of Pointers.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
Object Oriented Programming using C++ - Part 4
polymorphism in c++ with Full Explanation.
Programming Fundamentals
Unit 6 pointers
Pointers
INTRODUCTION TO POINTER IN c++ AND POLYMORPHISM
C Programming - Refresher - Part III
week14Pointers_II. pointers pemrograman dasar C++.pptx
FP 201 - Unit 6
Ad

More from MorfaSafi (8)

PDF
mechanical ventilation (0).pdfbhhhhhhhhhhj
PPTX
cpr.pptxhhhhghuuuuuuuyggffguiuuytdcgghhuuuytr
PDF
Tuberculosis v.pptx.pdfijjjhjjjjuuujuiiiiuyyh
PPTX
airway managment.pptxffggfdddr5tyyvcddser
PPTX
HM.pptxjjyyyyuuyhuhhhhhhhhyttrddddddtyhjjutrrr
PPTX
cpr.pptxgggggfhggyuugrdghyyttgyytffhyytgggy
PPTX
OXYGEN_.pptxfffhyrrredddddddddddggttrrrrer
PPT
Dyspepsia done by dr mohammed Sultan .ppt
mechanical ventilation (0).pdfbhhhhhhhhhhj
cpr.pptxhhhhghuuuuuuuyggffguiuuytdcgghhuuuytr
Tuberculosis v.pptx.pdfijjjhjjjjuuujuiiiiuyyh
airway managment.pptxffggfdddr5tyyvcddser
HM.pptxjjyyyyuuyhuhhhhhhhhyttrddddddtyhjjutrrr
cpr.pptxgggggfhggyuugrdghyyttgyytffhyytgggy
OXYGEN_.pptxfffhyrrredddddddddddggttrrrrer
Dyspepsia done by dr mohammed Sultan .ppt
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Basic Mud Logging Guide for educational purpose
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
O7-L3 Supply Chain Operations - ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Complications of Minimal Access Surgery at WLH
Insiders guide to clinical Medicine.pdf
Cell Types and Its function , kingdom of life
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
Classroom Observation Tools for Teachers
Module 4: Burden of Disease Tutorial Slides S2 2025
TR - Agricultural Crops Production NC III.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Basic Mud Logging Guide for educational purpose
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial diseases, their pathogenesis and prophylaxis

Lect 7 - 8 Pointers.pptffffffdfgfffddddg

  • 2. Pointers and dynamic objects/ Slide 2 Topics  Pointers  Memory addresses  Declaration  Dereferencing a pointer  Pointers to pointer
  • 3. Pointers and dynamic objects/ Slide 3 Computer Memory  Each variable is assigned a memory slot (the size depends on the data type) and the variable’s data is stored there Variable a’s value, i.e., 100, is stored at memory location 1024 100 100 … … … … 1024 1024 … … emory address: 1024 1032 int a = 100; … … 1020 a
  • 4. Pointers and dynamic objects/ Slide 4 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 100 … … … … 1024 1024 … … Memory address: 1024 1032 … … 1020 integer pointer
  • 5. Pointers and dynamic objects/ Slide 5 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
  • 6. Pointers and dynamic objects/ Slide 6 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; int **p; // pointer to pointer
  • 7. Pointers and dynamic objects/ Slide 7 Address Operator &  The "address of " operator (&) gives the memory address of the variable  Usage: &variable_name 100 100 … … … … … … … … Memory address: 1024 int a = 100; //get the value, cout << a; //prints 100 //get the memory address cout << &a; //prints 1024 … … 1020 a
  • 8. Pointers and dynamic objects/ Slide 8 Address Operator & 100 100 88 88 … … … … … … 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
  • 9. Pointers and dynamic objects/ Slide 9  The value of pointer p is the address of variable a  A pointer is also a variable, so it has its own memory address Pointer Variables 100 100 88 88 … … 1024 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
  • 10. Pointers and dynamic objects/ Slide 10 Pointer to Pointer What is the output? 58 58 58
  • 11. Pointers and dynamic objects/ Slide 11 Dereferencing Operator *  We can access to the value stored in the variable pointed to by using the dereferencing operator (*), 100 100 88 88 … … 1024 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
  • 12. Pointers and dynamic objects/ Slide 12 Reference Variables  A reference variable always refers to the same object. Assigning a reference variable with a new value actually changes the value of the referred object.  Reference variables are commonly used for parameter passing to a function
  • 13. Pointers and dynamic objects/ Slide 13 Pass by Reference void Swap(int &y, int &z) { int temp = y; y = z; z = temp; } int main() { int a = 99; int b = 55; Swap(a, b); cout << a << b << endl; return 0; }
  • 14. Pointers and dynamic objects/ Slide 14 Usage Pointer void Swap(int *Ptr1, int *Ptr2){ int temp = *Ptr1; *Ptr1 = *Ptr2; *Ptr2 = temp; } int main() { int a = 99; int b = 55; Swap(&a, &b); cout << a << b << endl; return 0; }
  • 15. Pointers and dynamic objects/ Slide 15 Another Example The code void doubleIt(int x, int * p) { *p = 2 * x; } int main() { int a = 16; doubleIt(9, &a); return 0; } Box diagram Memory Layout 9 x p (8200) x (8196) 16 a main doubleIt p a (8192) 16 9 8192 main doubleIt a gets 18
  • 16. Pointers and dynamic objects/ Slide 16 Pointers and Arrays  The name of an array points only to the first element not the whole array. 1000 1012 1016 1004 1008
  • 17. Pointers and dynamic objects/ Slide 17 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
  • 18. Pointers and dynamic objects/ Slide 18 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 22 a[4] a[0] a[2] a[1] a[3] a a This element is called a[0] or *a
  • 19. Pointers and dynamic objects/ Slide 19 Array Names as Pointers  To access an array, any pointer to the first element can be used instead of the name of the array. We could replace *p by *a 2 2 #include <iostream> using namespace std; void main(){ int a[5] = {2,4,6,8,22}; int *p = a; cout << a[0] << " " << *p; } 2 4 8 6 22 a[4] a[0] a[2] a[1] a[3] a p a
  • 20. Pointers and dynamic objects/ Slide 20 Multiple Array Pointers  Both a and p are pointers to the same array. 2 2 4 4 #include <iostream> using namespace std; void main(){ int a[5] = {2,4,6,8,22}; int *p = &a[1]; cout << a[0] << " " << p[-1]; cout << a[1] << " " << p[0]; } 2 4 8 6 22 a[4] a[0] a[2] a[1] a[3] p p[0] a[0]
  • 21. Pointers and dynamic objects/ Slide 21 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
  • 22. Pointers and dynamic objects/ Slide 22 *(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)  Note: flexible pointer syntax
  • 23. Pointers and dynamic objects/ Slide 23 Examples …
  • 24. Pointers and dynamic objects/ Slide 24 End

Editor's Notes

  • #15: Note the signature of main() – it’s an array parameter. And, like other examples we’ve given, it takes the number of elements as a parameter. Why?