Lesson 31
Lesson 31
OBJECTIVES: The student will define the terms: pointer variable, indirection,
and address.
INSTRUCTOR
NOTES: This lesson will provide a thorough introduction to pointer
variables, address, and indirection. Students were introduced to
pointers in Lesson 30 when learning how to work with this
pointers. The lesson will teach the use of the new and delete
commands and how to work with pointers. The idea of indirect
referencing takes some time to get used to. Be patient with the
students and encourage lots of questions during the lecture
presentation.
A. Pointer Variables
B. Syntax of Pointer Variables
C. Allocation of Dynamic Memory
D. Pointer Variable Manipulations
E. Dynamic Data Structures
Program 31-1
#include <iostream.h>
main()
{
int num = 5;
int *where; // where is a pointer, stores an address
a = 0;
a = NULL;
new type
*a = 5;
Diagram 31-1
a D2F6
D2F6 5
cout << a;
a 5
a = new int;
*a = 5;
b = a;
C6F2
a
(C6F2)
5
b
(C6F2)
a = new int;
b = new int;
*a = 8;
*b = 10;
*b = *a;
a 8
b 10 8
a = new int;
b = new int;
*a = 2;
*b = 5;
b = a;
a 2
b 5
a = new int;
b = new int;
*a = 2;
*b = 5;
delete b;
b = a;
a heap
2
b 5
more memory
a = 5;
1. During the next month of the course, you will learn how to use
pointer variables to build dynamic data structures.