Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

C++ for Engineers and Scientists, Fourth Edition 10-1

Chapter 10
Pointers

At a Glance

Instructor’s Manual Table of Contents


• Overview

• Objectives

• Teaching Tips

• Quick Quizzes

• Class Discussion Topics

• Additional Projects

• Additional Resources

• Key Terms
C++ for Engineers and Scientists, Fourth Edition 10-2

Lecture Notes

Overview
One of C++’s advantages is that it allows your students to access the addresses of variables used
in a program. This access gives programmers a view into a computer’s basic storage structure,
resulting in capabilities and programming power that isn’t available in other high-level
languages. This is accomplished by using a feature called pointers. Although other languages
provide pointers, C++ extends this feature by providing pointer arithmetic; that is, pointer values
can be added, subtracted, and compared.

Fundamentally, pointers are simply variables used to store memory addresses. This chapter
discusses the basics of declaring pointers, and then explains to your students methods of
applying pointer variables to access and use stored addresses in meaningful ways.

Objectives
In this chapter, students will learn about:
• Addresses and pointers
• Array names as pointers
• Pointer arithmetic
• Passing addresses
• Common programming errors

Teaching Tips
10.1 Addresses and Pointers
1. Introduce the C++ “address of” syntax as a preface to a discussion of pointers.

Storing Addresses

1. Introduce the concept of pointer variables used to store addresses.

Students often find the concept of a pointer difficult to visualize. It may be


Teaching
helpful to demonstrate pointers by drawing a box that contains an address as its
Tip
data value.

2. Review the provided sample program code in detail.


C++ for Engineers and Scientists, Fourth Edition 10-3

Using Addresses

1. Introduce the concept of the indirection operator.

2. Introduce the concept of indirect addressing.

Declaring Pointers

1. Introduce the concept of pointer declaration.

2. Review the provided sample program code in detail.

References and Pointers

1. Introduce the concept of a reference.

Students may not see reference syntax as particularly useful when the “address
Teaching
of” and “indirection operators” exist. However, reference syntax fits an
Tip
important role in helping to make programs less error prone and more secure.

2. Discuss automatic dereferencing.

3. Introduce the concept of reference variables.

4. Introduce the dereferencing operator.

5. Review the provided sample program code in detail.

Quick Quiz 1
1. True or False: The non-declarative expression &m in the C++ context evaluates to the
address of the variable m.
Answer: True

2. What operator provides the variable whose address is stored in a pointer variable?
Answer: indirection operator, *

10.2 Array Names as Pointers


1. Introduce the relationship between array names and pointers.
C++ for Engineers and Scientists, Fourth Edition 10-4

Teaching
In essence, an array name is a pointer, and can be used in much the same way.
Tip

2. Review the provided sample program code in detail.

3. Review the two ways that array elements can be referenced.

Dynamic Array Allocation

1. Introduce the concept of dynamic array allocation.

2. Introduce the new and delete operators

Students may find a discussion of the class destructor helpful as a way of


Teaching
ensuring that dynamically allocated class data members are returned to the
Tip
operating system when the class ceases to exist.

3. Review the provided sample program code in detail.

Quick Quiz 2
1. True or False: Using subscripts, the fourth element in an array, grade, is referenced as
grade[4].
Answer: False

2. True or False: A pointer is a variable used to store an address.


Answer: True

10.3 Pointer Arithmetic


1. Introduce the concept of pointer arithmetic.

2. Introduce the prefix and postfix operators in the context of pointer arithmetic.

Pre- and postfix operators are central to a C++ style of coding, but their use may
Teaching
introduce errors if operator precedence and operator functionality is not
Tip
understood.

3. Review the provided sample program code in detail.


C++ for Engineers and Scientists, Fourth Edition 10-5

Pointer Initialization

1. Introduce the concept of pointer initialization.

Quick Quiz 3
1. True or False: By adding and subtracting characters to pointers, you can obtain different
valid addresses.
Answer: False

2. True or False: Addresses can be incremented or decremented using the plus (+) or minus
(-) operators and the value is automatically scaled by the size of an array element.
Answer: True

10.4 Passing Addresses


1. Introduce the concept of passing addresses to a function.

2. Define the expression “pass by reference” in the C++ context.

3. Review the provided sample program code in detail.

Passing Arrays

1. Introduce the concept of passing arrays.

Teaching Students should understand that changes to array elements within a local
Tip function affect the array at the caller’s level of visibility.

2. Review the provided sample program code in detail.

Advanced Pointer Notation

1. Introduce the concept of advanced pointer notation in the context of C++ array element
referencing.

Quick Quiz 4
1. True or False: Passing a reference to a function is an implied use of an address despite the
fact that the actual call statement does not reveal what is being passed.
Answer: True
C++ for Engineers and Scientists, Fourth Edition 10-6

2. True or False: An advantage of using pointers rather than references is that the function
call specifies that addresses are being used.
Answer: True

10.5 Common Programming Errors


1. Review each of the listed programming errors in detail.

Quick Quiz 5
1. True or False: Attempting to store an address in a variable that has been declared a
pointer will result in an error.
Answer: False

2. True or False: Taking the address of pointer constants will cause an error.
Answer: True

Class Discussion Topics


1. Explore with your students why there are two ways to pass an address to a function: as a
pointer or as a reference.

2. Step through the swap() function in Program 10.11 and discuss why pointers are
helpful in this operation.

Additional Projects
1. Have the students write a small C++ program that uses pointers to functions. The
program passes a function pointer to a class method in order for that function to execute.

2. Have students code and test a findMin() function based on the findMax() function
in this chapter.

Additional Resources
1. Reference (C++):
http://www.cprogramming.com/tutorial/references.html
C++ for Engineers and Scientists, Fourth Edition 10-7

2. Pointers:
www.cplusplus.com/doc/tutorial/pointers.html

3. Arrays and Pointers:


http://www.fredosaurus.com/notes-cpp/arrayptr/26arraysaspointers.html

Key Terms
 Alias: The name for a variable that has been established through a reference declaration
 Automatically dereferenced: When an indirection operator is not required to retrieve a
value from a pointer
 Dynamic allocation: Creating space for a variable or array by using the new operator
 Implicitly dereferenced: A synonym for being automatically dereferenced
 Indirect addressing: The process of first going to the pointer for the address when using
a pointer variable to retrieve a value
 Indirection operator: The operator, *, that when followed by a pointer means “the
variable whose address is stored in”
 Offset: An index value that is scaled when used with a pointer to indicate an element in
an array
 Pass by reference: Explicitly passing addresses with the address operator
 Pointer: A variable used to store the address of another variable
 Reference: A named constant for an address
 Reference declaration: The syntax that permits a variable to be given additional names

You might also like