Professional Documents
Culture Documents
C For Engineers and Scientists 4th Edition Bronson Solutions Manual
C For Engineers and Scientists 4th Edition Bronson Solutions Manual
Chapter 10
Pointers
At a Glance
• Objectives
• Teaching Tips
• Quick Quizzes
• 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
Using Addresses
Declaring Pointers
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.
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, *
Teaching
In essence, an array name is a pointer, and can be used in much the same way.
Tip
Quick Quiz 2
1. True or False: Using subscripts, the fourth element in an array, grade, is referenced as
grade[4].
Answer: False
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.
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
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.
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
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
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
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