Pointer variable

C++ for
Engineers and Scientists
Second Edition
Chapter 12
Pointers
Objectives
•
•
•
•
•
Addresses and Pointers
Array Names as Pointers
Pointer Arithmetic
Passing Addresses
Common Programming Errors
C++ for Engineers and Scientists, Second Edition
2
Addresses and Pointers
• Address operator & placed in front of a variable
provides the memory address of the variable
C++ for Engineers and Scientists, Second Edition
3
Addresses and Pointers (continued)
• Pointer variable: used to store the address of
another variable
• Indirection operator (*): when followed by a
pointer, it means “the variable whose address is
stored in”
Example:
numAddr = &num // numAddr contains address
// of num
*numAddr
// numAddr points to num
C++ for Engineers and Scientists, Second Edition
4
Addresses and Pointers (continued)
• To declare a pointer, specify the type of the
variable to which it will point
Syntax:
dataType *pointerName;
Example:
int *numAddr;
C++ for Engineers and Scientists, Second Edition
5
Addresses and Pointers (continued)
C++ for Engineers and Scientists, Second Edition
6
Addresses and Pointers (continued)
• Declaration of a pointer includes the type of
variable it points to; indicates how much
storage to retrieve
C++ for Engineers and Scientists, Second Edition
7
Addresses and Pointers (continued)
• References vs. pointers:
– Reference is a named constant for an address
and cannot be modified
– Pointer is a variable and can be modified
– References are said to be automatically
dereferenced or implicitly dereferenced
– Pointers must be explicitly dereferenced using
the dereferencing operator *
C++ for Engineers and Scientists, Second Edition
8
Addresses and Pointers (continued)
• To pass a simple variable as a function
argument, use a reference
• When dynamic memory allocation is required,
use pointers
• References: can be used as formal function
parameters and function return types, and also
as variables
• Reference variable: a new name for an existing
variable (both point to the same location); also
called an alias
C++ for Engineers and Scientists, Second Edition
9
Addresses and Pointers (continued)
Syntax:
dataType& aliasName = variableName;
C++ for Engineers and Scientists, Second Edition
10
Addresses and Pointers (continued)
• Original variable or its alias can be used
• Reference must be declared with same data
type as original variable
• Reference cannot be equated with a constant
• Reference cannot be changed to point to
another variable
• Multiple references can be declared on the
same line; each must be preceded by &
C++ for Engineers and Scientists, Second Edition
11
Addresses and Pointers (continued)
• Compiler automatically uses the variable when
a new value is assigned to the reference
Example:
int b:
// original variable
int& a=b; // a is a reference variable
// with b’s address
a = 10; // change b’s value to 10
• Compiler automatically performs an indirect
access of b’s value without explicitly using
indirection symbol *; called automatic
dereference
C++ for Engineers and Scientists, Second Edition
12
Addresses and Pointers (continued)
• Same example with a pointer:
int b;
// declare integer variable
int *a = &b; // declare pointer a,
// store b’s address
*a = 10; // changes b’s value to 10
• This example uses explicit dereference
• Pointer can be changed to point to another
variable
• Indirection operator * must be used; also
called dereferencing operator
C++ for Engineers and Scientists, Second Edition
13
Array Names as Pointers
• Internally, the compiler uses addresses to
access specific array elements
Example: integer array grade
• To call grade [3], the compiler computes:
&grade[3] = &grade[0] + (3 * sizeof(int))
C++ for Engineers and Scientists, Second Edition
14
Array Names as Pointers (continued)
C++ for Engineers and Scientists, Second Edition
15
Array Names as Pointers (continued)
• Array elements may be referenced in two
ways:
C++ for Engineers and Scientists, Second Edition
16
Array Names as Pointers (continued)
C++ for Engineers and Scientists, Second Edition
17
Array Names as Pointers (continued)
• When an array is created, the array name
becomes the name of a pointer constant
containing the address of the first location in the
array
C++ for Engineers and Scientists, Second Edition
18
Array Names as Pointers (continued)
C++ for Engineers and Scientists, Second Edition
19
Array Names as Pointers (continued)
• Array name and pointer can be used
interchangeably in most respects, except:
– True pointer is a variable, and its contents (an
address) can be changed
– Array name is a pointer constant, and its
contents (an address) cannot be changed
• Array declared with size causes static
allocation of memory for all the elements,
whether used or not
C++ for Engineers and Scientists, Second Edition
20
Array Names as Pointers (continued)
• Dynamic allocation allows the allocated
memory to be determined and adjusted at run
time, using new and delete operators
• Explicit dynamic requests are made in a
declaration or assignment statement
C++ for Engineers and Scientists, Second Edition
21
Array Names as Pointers (continued)
C++ for Engineers and Scientists, Second Edition
22
Pointer Arithmetic
• Arithmetic operations can be done on pointer
variables (as long as a valid address results)
• For arrays, the compiler will scale the arithmetic
operation to ensure that the result points to a
value of the correct type
• Addresses can be incremented or decremented
using prefix and postfix increment and
decrement operators
C++ for Engineers and Scientists, Second Edition
23
Pointer Arithmetic (continued)
C++ for Engineers and Scientists, Second Edition
24
Pointer Arithmetic (continued)
• Relational operators can be used to compare
addresses in pointers
C++ for Engineers and Scientists, Second Edition
25
Pointer Arithmetic (continued)
• Pointers can be initialized when declared
• If pointing to a variable, the variable must be
declared first
• Pointers to arrays can be initialized when
declared in two ways
Example:
double volts[5];
// declare array
double *zing = &volts[0]; // first way
double *zing = volts;
// second way
C++ for Engineers and Scientists, Second Edition
26
Passing Addresses
• Addresses can be implicitly passed to functions
using reference variables
• Addresses can be explicitly passed to functions
using pointers; this is a pass by reference
• Place the address operator & in front of the
variable to pass its address
Example:
swap(&firstnum, &secnum)
C++ for Engineers and Scientists, Second Edition
27
Passing Addresses (continued)
• Function must be created with pointer arguments
Example:
void swap(double *nm1Addr, double *nm2Addr);
• When an array is passed to a function, only its
address is actually passed (an entire array is
always passed by reference)
• The address of the array is also the address of
element 0
C++ for Engineers and Scientists, Second Edition
28
Passing Addresses (continued)
C++ for Engineers and Scientists, Second Edition
29
Passing Addresses (continued)
C++ for Engineers and Scientists, Second Edition
30
Passing Addresses (continued)
• Pointer notation can be used for
multidimensional arrays, but it becomes
complex as the array dimensions increase
C++ for Engineers and Scientists, Second Edition
31
Passing Addresses (continued)
• An equivalent set of relationships:
C++ for Engineers and Scientists, Second Edition
32
Passing Addresses (continued)
• Function name is also a pointer
• Pointer can be declared to point to a function
name
C++ for Engineers and Scientists, Second Edition
33
Common Programming Errors
• Attempting to store an address in a non-pointer
variable
• Using a pointer to access non-existent array
elements (out of bounds)
• Failing to use the bracket set [] after the
delete operator when dynamically
deallocating memory previously allocated by
the new[] operator
• Incorrectly applying the address and indirection
operators
C++ for Engineers and Scientists, Second Edition
34
Common Programming Errors
(continued)
• Taking the address of a pointer constant (which
is already an address)
• Taking the address of a reference argument,
reference variable, or register variable (which
are already addresses)
• Initializing pointer variables incorrectly
• Failing to understand if a variable contains an
address or is an address
C++ for Engineers and Scientists, Second Edition
35
Summary
• Address of a variable can be obtained using the
address operator &
• Pointer variable stores the address of another
variable
• Array name is a pointer constant
• Access to an array element using subscript
notation can be replaced using pointer notation
• Arrays can be dynamically created at run time
C++ for Engineers and Scientists, Second Edition
36
Summary (continued)
• Arrays are passed to functions as addresses
• When passing a single-dimensional array to a
function, the function argument can be declared
as a pointer or as an array
• Pointers can be incremented, decremented,
compared, and assigned
• Values added to or subtracted from a pointer
are automatically scaled based on the data type
C++ for Engineers and Scientists, Second Edition
37