SlideShare a Scribd company logo
1 of 23
A First Book of C++A First Book of C++
Chapter 8Chapter 8
Arrays and PointersArrays and Pointers
∗ In this chapter, you will learn about:
∗ Introduction to Pointers
∗ Array Names as Pointers
∗ Pointer Arithmetic
∗ Passing Addresses
∗ Common Programming Errors
A First Book of C++ 4th Edition 2
ObjectivesObjectives
∗ Three major items of a variable:
∗ Value stored
∗ Number of bytes reserved
∗ Where in memory these bytes are located
A First Book of C++ 4th Edition 3
Introduction to PointersIntroduction to Pointers
∗ Programmers are usually only concerned with a
variable’s value, not its address
∗ Address operator &: determines the address of a
variable
∗ & means “address of”
∗ When placed in front of variable num, is translated as
“the address of num” (&num)
∗ Program 8.2 uses the address operator to display the
address of variable num
A First Book of C++ 4th Edition 4
Introduction to Pointers (cont'd.)Introduction to Pointers (cont'd.)
A First Book of C++ 4th Edition 5
Introduction to Pointers (cont'd.)
A First Book of C++ 4th Edition 6
Introduction to Pointers (cont'd.)Introduction to Pointers (cont'd.)
value
size
location
∗ Address can be stored in suitably declared variables
∗ Example:
int *numAddr; //declare pointer
numAddr = # //store addr into pointer
∗Statement stores address of num in variable numAddr
∗numAddr is a pointer
A First Book of C++ 4th Edition 7
Storing AddressesStoring Addresses
A First Book of C++ 4th Edition 8
Storing Addresses (cont'd.)
∗ Indirection Operator * : the * symbol, when followed
by a pointer, means “the variable whose address is
stored in”
∗ If y is a pointer, then *y means “the variable whose
address is stored in y”
∗ Commonly shortened to “the variable pointed to by y”
∗ Example (Figure 8.6):
∗ The content of y is the address mmmm
∗ The variable pointed to by y is qqqq
∗ The content at address mmmm is qqqq
A First Book of C++ 4th Edition 9
Using Addresses
A First Book of C++ 4th Edition 10
Using Addresses (cont'd.)
*y
Indirect Addressing
∗ Using a pointer requires a double lookup
∗ First an address is retrieved
∗ Address is used to retrieve actual data
∗ Why store addresses if data can be retrieved in one
step using variable’s name?
∗ Pointers make it possible to create and delete new
storage locations dynamically during program
execution
A First Book of C++ 4th Edition 11
Using Addresses (cont'd.)
∗ Pointers must be declared before they can store an
address
∗ Example: if address in pointer numAddr is the
address of an integer, the declaration is:
int *numAddr;
∗ This declaration is read as “the variable pointed to by
numAddr is an integer”
∗ The declaration specifies:
∗ The variable pointed to by numAddr is an integer
∗ numAddr is a pointer (because it is used with the
indirection operator *)
A First Book of C++ 4th Edition 12
Declaring Pointers
A First Book of C++ 4th Edition 13
Declaring Pointers (cont'd.)
char c;
int val;
A First Book of C++ 4th Edition 14
∗ Program 8.3 output:
The address stored in numAddr is 0012FEC8
The value pointed to by numAddr is 22
The address now stored in numAddr is
0012FEBC
The value now pointed to by numAddr is 158
A First Book of C++ 4th Edition 15
Declaring Pointers (cont'd.)
∗ Program 8.3 actions:
∗ Declaration statement int *numAddr;
∗ declares numAddr as pointer variable storing the address
of an integer variable
∗ Data type determines pointer storage requirements
∗ Statement numAddr = &miles;
∗ stores address of variable miles into pointer numAddr
∗ First cout statement: displays address
∗ Second cout statement: uses indirection operator (*)
to retrieve and display the value pointed to by numAddr
(the value stored in miles)
A First Book of C++ 4th Edition 16
Declaring Pointers (cont'd.)
∗ Program 8.3 actions:
∗ Statement numAddr = &dist;
∗ changes numAddr’s value to address of variable dist
∗ This is allowed because the pointer numAddr can be used
to point to any integer value (miles and dist are both
integer values)
∗ Last two cout statements:
∗ Verify change in numAddr value
∗ Confirm that new stored address points to variable dist
A First Book of C++ 4th Edition 17
Declaring Pointers (cont'd.)
A First Book of C++ 4th Edition 18
Declaring Pointers (cont'd.)
∗ Reference: named constant for an address
∗ Address named as a reference can’t be altered after the
address has been assigned
∗ Automatic dereference: an indirect access of a
variable’s value without using the indirection
operator symbol (*)
∗ Instead, uses reference pointer(&)
A First Book of C++ 4th Edition 19
References and Pointers
∗ Example of automatic dereference (get values of
pointers):
int b; // b is an integer variable
int &a = b; // a is a reference variable
that stores b's address
a = 10; // this changes b's value to 10
∗ Statement int &a = b;
∗ a declared a reference variable
∗ Compiler assigns address of b (not the contents of b)
∗ Statement a = 10; compiler uses address stored in a
to change the value stored in b to 10
A First Book of C++ 4th Edition 20
References and Pointers (cont'd.)
∗ Repeat of previous example using pointers instead of
automatic dereferencing
int b; // b is an integer variable
int *a = &b; // a is a pointer - store
// b's address in a
*a = 10; // this changes b's value
to 10
∗ a is a pointer initialized to store address of b
A First Book of C++ 4th Edition 21
References and Pointers (cont'd.)
∗ Pointer a can be altered to point to a different variable
int b, c; // b is an integer variable
int *a = &b; // a is a pointer – store b's address in a
*a = 10; // this changes b's value to 10
int *a = &c; // a is a pointer – store c's address in a
*a = 15; // this changes c's value to 15
∗ Reference variable a (from previous example) cannot be altered to refer to any
variable except one to which it was initialized
int b, c; // b is an integer variable
int &a = b; // a is a reference variable that store b's
// address
a = 10; // this changes b's value to 10
int &a = c; // this is an invalid assignment
a = 15; // this is an invalid assignment
A First Book of C++ 4th Edition 22
References and Pointers (cont'd.)
//Program 8.4
#include <iostream>
using namespace std;
int main()
{
double total = 20.5; //declare and initialize total
double &sum = total; // declare another name for total
cout << "sum = " << sum << endl;
sum = 18.6; //changes the value of total
cout << "total = " << total << endl;
return 0;
}
References and Pointers (cont'd.)
A First Book of C++ 4th Edition 23

More Related Content

What's hot

Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...hwbloom42
 
Three address code generation
Three address code generationThree address code generation
Three address code generationRabin BK
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer웅식 전
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06Niit Care
 

What's hot (20)

Session 5
Session 5Session 5
Session 5
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Pointers
PointersPointers
Pointers
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Pointers
PointersPointers
Pointers
 
pointers
pointerspointers
pointers
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
Ch9b
Ch9bCh9b
Ch9b
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
String .h
String .hString .h
String .h
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06
 

Similar to Csc1100 lecture10 ch08_pt1

Similar to Csc1100 lecture10 ch08_pt1 (20)

pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
Pointers
PointersPointers
Pointers
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Pointers
PointersPointers
Pointers
 
C- language Lecture 5
C- language Lecture 5C- language Lecture 5
C- language Lecture 5
 
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
PointersPointers
Pointers
 
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
Pointers in CPointers in C
Pointers in C
 
Pointers
PointersPointers
Pointers
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
C pointer
C pointerC pointer
C pointer
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
Pointer
PointerPointer
Pointer
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 

More from IIUM

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhostIIUM
 
Chapter 2
Chapter 2Chapter 2
Chapter 2IIUM
 
Chapter 1
Chapter 1Chapter 1
Chapter 1IIUM
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimediaIIUM
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblockIIUM
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice keyIIUM
 
Group p1
Group p1Group p1
Group p1IIUM
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoIIUM
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009IIUM
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lformsIIUM
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answerIIUM
 
Redo midterm
Redo midtermRedo midterm
Redo midtermIIUM
 
Heaps
HeapsHeaps
HeapsIIUM
 
Report format
Report formatReport format
Report formatIIUM
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelinesIIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam PaperIIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam PaperIIUM
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516IIUM
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotationsIIUM
 
Week12 graph
Week12   graph Week12   graph
Week12 graph IIUM
 

More from IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Heaps
HeapsHeaps
Heaps
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 

Csc1100 lecture10 ch08_pt1

  • 1. A First Book of C++A First Book of C++ Chapter 8Chapter 8 Arrays and PointersArrays and Pointers
  • 2. ∗ In this chapter, you will learn about: ∗ Introduction to Pointers ∗ Array Names as Pointers ∗ Pointer Arithmetic ∗ Passing Addresses ∗ Common Programming Errors A First Book of C++ 4th Edition 2 ObjectivesObjectives
  • 3. ∗ Three major items of a variable: ∗ Value stored ∗ Number of bytes reserved ∗ Where in memory these bytes are located A First Book of C++ 4th Edition 3 Introduction to PointersIntroduction to Pointers
  • 4. ∗ Programmers are usually only concerned with a variable’s value, not its address ∗ Address operator &: determines the address of a variable ∗ & means “address of” ∗ When placed in front of variable num, is translated as “the address of num” (&num) ∗ Program 8.2 uses the address operator to display the address of variable num A First Book of C++ 4th Edition 4 Introduction to Pointers (cont'd.)Introduction to Pointers (cont'd.)
  • 5. A First Book of C++ 4th Edition 5 Introduction to Pointers (cont'd.)
  • 6. A First Book of C++ 4th Edition 6 Introduction to Pointers (cont'd.)Introduction to Pointers (cont'd.) value size location
  • 7. ∗ Address can be stored in suitably declared variables ∗ Example: int *numAddr; //declare pointer numAddr = &num; //store addr into pointer ∗Statement stores address of num in variable numAddr ∗numAddr is a pointer A First Book of C++ 4th Edition 7 Storing AddressesStoring Addresses
  • 8. A First Book of C++ 4th Edition 8 Storing Addresses (cont'd.)
  • 9. ∗ Indirection Operator * : the * symbol, when followed by a pointer, means “the variable whose address is stored in” ∗ If y is a pointer, then *y means “the variable whose address is stored in y” ∗ Commonly shortened to “the variable pointed to by y” ∗ Example (Figure 8.6): ∗ The content of y is the address mmmm ∗ The variable pointed to by y is qqqq ∗ The content at address mmmm is qqqq A First Book of C++ 4th Edition 9 Using Addresses
  • 10. A First Book of C++ 4th Edition 10 Using Addresses (cont'd.) *y Indirect Addressing
  • 11. ∗ Using a pointer requires a double lookup ∗ First an address is retrieved ∗ Address is used to retrieve actual data ∗ Why store addresses if data can be retrieved in one step using variable’s name? ∗ Pointers make it possible to create and delete new storage locations dynamically during program execution A First Book of C++ 4th Edition 11 Using Addresses (cont'd.)
  • 12. ∗ Pointers must be declared before they can store an address ∗ Example: if address in pointer numAddr is the address of an integer, the declaration is: int *numAddr; ∗ This declaration is read as “the variable pointed to by numAddr is an integer” ∗ The declaration specifies: ∗ The variable pointed to by numAddr is an integer ∗ numAddr is a pointer (because it is used with the indirection operator *) A First Book of C++ 4th Edition 12 Declaring Pointers
  • 13. A First Book of C++ 4th Edition 13 Declaring Pointers (cont'd.) char c; int val;
  • 14. A First Book of C++ 4th Edition 14
  • 15. ∗ Program 8.3 output: The address stored in numAddr is 0012FEC8 The value pointed to by numAddr is 22 The address now stored in numAddr is 0012FEBC The value now pointed to by numAddr is 158 A First Book of C++ 4th Edition 15 Declaring Pointers (cont'd.)
  • 16. ∗ Program 8.3 actions: ∗ Declaration statement int *numAddr; ∗ declares numAddr as pointer variable storing the address of an integer variable ∗ Data type determines pointer storage requirements ∗ Statement numAddr = &miles; ∗ stores address of variable miles into pointer numAddr ∗ First cout statement: displays address ∗ Second cout statement: uses indirection operator (*) to retrieve and display the value pointed to by numAddr (the value stored in miles) A First Book of C++ 4th Edition 16 Declaring Pointers (cont'd.)
  • 17. ∗ Program 8.3 actions: ∗ Statement numAddr = &dist; ∗ changes numAddr’s value to address of variable dist ∗ This is allowed because the pointer numAddr can be used to point to any integer value (miles and dist are both integer values) ∗ Last two cout statements: ∗ Verify change in numAddr value ∗ Confirm that new stored address points to variable dist A First Book of C++ 4th Edition 17 Declaring Pointers (cont'd.)
  • 18. A First Book of C++ 4th Edition 18 Declaring Pointers (cont'd.)
  • 19. ∗ Reference: named constant for an address ∗ Address named as a reference can’t be altered after the address has been assigned ∗ Automatic dereference: an indirect access of a variable’s value without using the indirection operator symbol (*) ∗ Instead, uses reference pointer(&) A First Book of C++ 4th Edition 19 References and Pointers
  • 20. ∗ Example of automatic dereference (get values of pointers): int b; // b is an integer variable int &a = b; // a is a reference variable that stores b's address a = 10; // this changes b's value to 10 ∗ Statement int &a = b; ∗ a declared a reference variable ∗ Compiler assigns address of b (not the contents of b) ∗ Statement a = 10; compiler uses address stored in a to change the value stored in b to 10 A First Book of C++ 4th Edition 20 References and Pointers (cont'd.)
  • 21. ∗ Repeat of previous example using pointers instead of automatic dereferencing int b; // b is an integer variable int *a = &b; // a is a pointer - store // b's address in a *a = 10; // this changes b's value to 10 ∗ a is a pointer initialized to store address of b A First Book of C++ 4th Edition 21 References and Pointers (cont'd.)
  • 22. ∗ Pointer a can be altered to point to a different variable int b, c; // b is an integer variable int *a = &b; // a is a pointer – store b's address in a *a = 10; // this changes b's value to 10 int *a = &c; // a is a pointer – store c's address in a *a = 15; // this changes c's value to 15 ∗ Reference variable a (from previous example) cannot be altered to refer to any variable except one to which it was initialized int b, c; // b is an integer variable int &a = b; // a is a reference variable that store b's // address a = 10; // this changes b's value to 10 int &a = c; // this is an invalid assignment a = 15; // this is an invalid assignment A First Book of C++ 4th Edition 22 References and Pointers (cont'd.)
  • 23. //Program 8.4 #include <iostream> using namespace std; int main() { double total = 20.5; //declare and initialize total double &sum = total; // declare another name for total cout << "sum = " << sum << endl; sum = 18.6; //changes the value of total cout << "total = " << total << endl; return 0; } References and Pointers (cont'd.) A First Book of C++ 4th Edition 23