Chapter 9 - Pointers: Starting Out With C++, 3 Edition
Chapter 9 - Pointers: Starting Out With C++, 3 Edition
1
Starting Out with C++, 3rd Edition
9.1 Getting the address of a Variable
2
Starting Out with C++, 3rd Edition
Figure 9-1
3
Starting Out with C++, 3rd Edition
Program 9-1
// This program uses the & operator to determine a variable’s
// address and the sizeof operator to determine its size.
#include <iostream.h>
void main(void)
{
int x = 25;
cout << "The address of x is " << &x << endl;
cout << "The size of x is " << sizeof(x) << " bytes\n";
cout << "The value in x is " << x << endl;
}
4
Starting Out with C++, 3rd Edition
Program Output
5
Starting Out with C++, 3rd Edition
Pointer Variables
6
Starting Out with C++, 3rd Edition
Pointers are useful for the following:
void main(void)
{
int x = 25;
int *ptr;
8
Starting Out with C++, 3rd Edition
Program Output
The value in x is 25
The address of x is 0x7e00
9
Starting Out with C++, 3rd Edition
Figure 9-2
x
25
ptr
0x7e00
Address of x: 0x7e00
10
Starting Out with C++, 3rd Edition
Program 9-3
// This program demonstrates the use of the indirection
// operator.
#include <iostream.h>
void main(void)
{
int x = 25;
int *ptr;
11
Starting Out with C++, 3rd Edition
Program Output
12
Starting Out with C++, 3rd Edition
Program 9-4
#include <iostream>
void main(void)
{
int x = 25, y = 50, z = 75;
int *ptr;
cout << "Here are the values of x, y, and z:\n";
cout << x << " " << y << " " << z << endl;
ptr = &x; // Store the address of x in ptr
*ptr *= 2; // Multiply value in x by 2
ptr = &y; // Store the address of y in ptr
*ptr *= 2; // Multiply value in y by 2
ptr = &z; // Store the address of z in ptr
*ptr *= 2; // Multiply value in z by 2
cout << "Once again, here are the values of x, y, and z:\n";
cout << x << " " << y << " " << z << endl;
}
13
Starting Out with C++, 3rd Edition
Program Output
14
Starting Out with C++, 3rd Edition
9.3 Relationship Between Arrays and
Pointers
• array names can be used as pointers, and
vice-versa.
15
Starting Out with C++, 3rd Edition
Program 9-5
// This program shows an array name being dereferenced
// with the * operator.
#include <iostream.h>
void main(void)
{
short numbers[] = {10, 20, 30, 40, 50};
16
Starting Out with C++, 3rd Edition
Program Output
17
Starting Out with C++, 3rd Edition
Figure 9-3
numbers
18
Starting Out with C++, 3rd Edition
Figure 9-4
19
Starting Out with C++, 3rd Edition
Program 9-6
// This program processes the contents of an array. Pointer
// notation is used.
#include <iostream.h>
void main(void)
{
int numbers[5];
20
Starting Out with C++, 3rd Edition
Program Output with Example Input
21
Starting Out with C++, 3rd Edition
Program 9-7
// This program uses subscript notation with a pointer and
// pointer notation with an array name.
#include <iostream.h>
void main(void)
{
float coins[5] = {0.05, 0.1, 0.25, 0.5, 1.0};
float *floatPtr; // Pointer to a float
int count; // array index
22
Starting Out with C++, 3rd Edition
Program continues
for (count = 0; count < 5; count++)
cout << floatPtr[count] << " ";
cout << "\nAnd here they are again:\n";
for (count = 0; count < 5; count++)
cout << *(coins + count) << " ";
cout << endl;
}
23
Starting Out with C++, 3rd Edition
Program Output
24
Starting Out with C++, 3rd Edition
Program 9-8
// This program uses the address of each element in
the array.
#include <iostream.h>
#include <iomanip.h>
void main(void)
{
float coins[5] = {0.05, 0.1, 0.25, 0.5, 1.0};
float *floatPtr; // Pointer to a float
int count; // array index
cout.precision(2);
cout << "Here are the values in the coins array:\n";
25
Starting Out with C++, 3rd Edition
Program continues
for (count = 0; count < 5; count++)
{
floatPtr = &coins[count];
cout << *floatPtr << " ";
}
cout << endl;
}
26
Starting Out with C++, 3rd Edition
Program Output
27
Starting Out with C++, 3rd Edition
9.4 Pointer Arithmetic
void main(void)
{
int set[8] = {5, 10, 15, 20, 25, 30, 35, 40};
int *nums, index;
nums = set;
cout << "The numbers in set are:\n";
for (index = 0; index < 8; index++)
{
cout << *nums << " ";
nums++;
}
29
Starting Out with C++, 3rd Edition
Program continues
30
Starting Out with C++, 3rd Edition
Program Output
31
Starting Out with C++, 3rd Edition
9.5 Initializing Pointers
32
Starting Out with C++, 3rd Edition
9.6 Comparing Pointers
33
Starting Out with C++, 3rd Edition
Figure 9-5
(Addresses)
34
Starting Out with C++, 3rd Edition
Program 9-10
// This program uses a pointer to display the contents
// of an integer array.
#include <iostream.h>
void main(void)
{
int set[8] = {5, 10, 15, 20, 25, 30, 35, 40};
int *nums = set; // Make nums point to set
35
Starting Out with C++, 3rd Edition
Program continues
36
Starting Out with C++, 3rd Edition
Program Output
37
Starting Out with C++, 3rd Edition
9.7 Pointers as Function Parameters
38
Starting Out with C++, 3rd Edition
Program 9-11
// This program uses two functions that accept addresses of
// variables as arguments.
#include <iostream.h>
// Function prototypes
void getNumber(int *);
void doubleValue(int *);
void main(void)
{
int number;
getNumber(&number) // Pass address of number to getNumber
doubleValue(&number); // and doubleValue.
cout << "That value doubled is " << number << endl;
}
39
Starting Out with C++, 3rd Edition
Program continues
// Definition of getNumber. The parameter, Input, is a pointer.
// This function asks the user for a number. The value entered
// is stored in the variable pointed to by Input.
40
Starting Out with C++, 3rd Edition
Program Output with Example Input
41
Starting Out with C++, 3rd Edition
Program 9-12
// This program demonstrates that a pointer may be used as a
// parameter to accept the address of an array. Either subscript
// or pointer notation may be used.
#include <iostream.h>
#include <iomanip.h>
// Function prototypes
void getSales(float *);
float totalSales(float *);
void main(void)
{
float sales[4];
getSales(sales);
cout.precision(2);
42
Starting Out with C++, 3rd Edition
Program continues
cout.setf(ios::fixed | ios::showpoint);
cout << "The total sales for the year are $";
cout << totalSales(sales) << endl;
}
43
Starting Out with C++, 3rd Edition
Program continues
// Definition of totalSales. This function uses a pointer to
// accept the address of an array of four floats. The function
// gets the total of the elements in the array and returns that
// value. (Pointer notation is used in this function.)
44
Starting Out with C++, 3rd Edition
Program Output with Example Input
45
Starting Out with C++, 3rd Edition
9.8 Focus on Software Engineering:
Dynamic Memory Allocation
• Variables may be created and destroyed
while a program is running.
• A pointer than contains the address 0 is
called a null pointer.
• Use the new operator to dynamically
allocate memory.
• Use delete to dynamically deallocate
memory.
46
Starting Out with C++, 3rd Edition
Program 9-13
// This program totals and averages the sales figures for any
// number of days. The figures are stored in a dynamically
// allocated array.
#include <iostream.h>
#include <iomanip.h>
void main(void)
{
float *sales, total = 0, average;
int numDays;
cout << "How many days of sales figures do you wish ";
cout << "to process? ";
cin >> numDays;
sales = new float[numDays]; // Allocate memory
47
Starting Out with C++, 3rd Edition
Program continues
if (sales == NULL) // Test for null pointer
{
cout << "Error allocating memory!\n";
return;
}
// Get the sales figures from the user
cout << "Enter the sales figures below.\n";
for (int count = 0; count < numDays; count++)
{
cout << "Day " << (count + 1) << ": ";
cin >> sales[count];
}
// Calculate the total sales
for (count = 0; count < numDays; count++)
{
total += sales[count];
}
48
Starting Out with C++, 3rd Edition
Program continues
49
Starting Out with C++, 3rd Edition
Program Output with Example Input
50
Starting Out with C++, 3rd Edition
9.9 Focus on Software Engineering:
Returning Pointers from Functions
• Functions can return pointers, but you must
be sure the object the pointer references still
exists.
• You should only return a pointer from a
function if it is:
– A pointer to an object that was passed into the
function as an argument.
– A pointer to a dynamically allocated object.
51
Starting Out with C++, 3rd Edition