of 51
8/6/2019 ITEI303- Lecture 2 Pointers in C++
1/51
Starting Out with C++, 3rdEdition1
Review on Pointers in C++
Engr. Julius S. Cansino
8/6/2019 ITEI303- Lecture 2 Pointers in C++
2/51
Starting Out with C++, 3rdEdition2
9.1 Getting the address of a Variable
The address operator (&) returns the
memory address of a variable.
8/6/2019 ITEI303- Lecture 2 Pointers in C++
3/51
Starting Out with C++, 3rdEdition3
Figure
9-1
1200 1201 1203
letter number amount
8/6/2019 ITEI303- Lecture 2 Pointers in C++
4/51
Starting Out with C++, 3rdEdition4
Program 9-1// This programuses the & operatorto determinea variables
// address and the sizeofoperatorto determine its size.
#include
void main(void)
{int x = 25;
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
5/51
Starting Out with C++, 3rdEdition5
3URJUDP2XWSXW
The address of x is 0x8f05The size of x is 2 bytes
The value in x is 25
8/6/2019 ITEI303- Lecture 2 Pointers in C++
6/51
Starting Out with C++, 3rdEdition6
Pointer Variables
Pointer variables, which are often just called
pointers, are designed to hold memory
addresses. With pointer variables you can
indirectly manipulate data stored in other
variables.
8/6/2019 ITEI303- Lecture 2 Pointers in C++
7/51
Starting Out with C++, 3rdEdition7
Pointers are useful for the following:
Working with memory locations that
regular variables dont give you access to
Working with strings and arrays
Creating new variables in memory while the
program is running
Creating arbitrarily-sized lists of values in
memory
8/6/2019 ITEI303- Lecture 2 Pointers in C++
8/51
Starting Out with C++, 3rdEdition
8
Program 9-2
// This program stores theaddress ofa variable ina pointer.#include
void main(void)
{
int x = 25;
int *ptr;
ptr = &x; // Storetheaddress of x in ptr
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
9/51
Starting Out with C++, 3rdEdition
9
3URJUDP2XWSXW
The value in x is 25
The address of x is 0x7e00
8/6/2019 ITEI303- Lecture 2 Pointers in C++
10/51
Starting Out with C++, 3rdEdition10
F
igure9-2
0x7e00
25
ptr
x
Address of x: 0x7e00
8/6/2019 ITEI303- Lecture 2 Pointers in C++
11/51
Starting Out with C++, 3rdEdition11
Program 9-3// This program demonstrates theuseofthe indirection
// operator.
#include
void main(void)
{
int x = 25;int *ptr;
ptr = &x; // Storetheaddress of x in ptr
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
12/51
Starting Out with C++, 3rdEdition12
3URJUDP2XWSXW
Here is the value in x, printed twice:25 25Once again, here is the value in x:
100 100
8/6/2019 ITEI303- Lecture 2 Pointers in C++
13/51
Starting Out with C++, 3rdEdition13
Program 9-4#include
void main(void)
{
int x = 25, y = 50, z = 75;
int *ptr;
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
14/51
Starting Out with C++, 3rdEdition14
3URJUDP2XWSXW
Here are the values of x, y, and z:25 50 75Once again, here are the values of x, y , and z:
50 100 150
8/6/2019 ITEI303- Lecture 2 Pointers in C++
15/51
Starting Out with C++, 3rdEdition15
9.3 Relationship Between Arrays and
Pointers
array names can be used as pointers, and
vice-versa.
8/6/2019 ITEI303- Lecture 2 Pointers in C++
16/51
Starting Out with C++, 3rdEdition16
Program 9-5
// This program shows anarraynamebeing dereferenced
// with the * operator.
#include
void main(void)
{
shortnumbers[] = {10, 20, 30, 40, 50};
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
17/51
Starting Out with C++, 3rdEdition17
3URJUDP2XWSXW
The first element in the array is 10
8/6/2019 ITEI303- Lecture 2 Pointers in C++
18/51
Starting Out with C++, 3rdEdition18
F
igure9-3
numbers
numbers[0] numbers[1] numbers[2] numbers[3] numbers[4]
8/6/2019 ITEI303- Lecture 2 Pointers in C++
19/51
Starting Out with C++, 3rdEdition19
F
igure9-4
numbers
numbers[0] numbers[1] numbers[2] numbers[3] numbers[4]
(numbers+1) (numbers+2) (numbers+3) (numbers+4)
8/6/2019 ITEI303- Lecture 2 Pointers in C++
20/51
Starting Out with C++, 3rdEdition
20
Program 9-6// This program processes thecontents ofanarray. Pointer
// notation is used.
#include
void main(void)
{
intnumbers[5];
cout > *(numbers + count);
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
21/51
Starting Out with C++, 3rdEdition
21
3URJUDP2XWSXWZLWK([DPSOH,QSXW
Enter five numbers: >(QWHU@Here are the numbers you entered:
510152025
8/6/2019 ITEI303- Lecture 2 Pointers in C++
22/51
Starting Out with C++, 3rdEdition
22
Program 9-7// This programuses subscriptnotationwith a pointerand
// pointernotationwith anarrayname.
#include
void main(void)
{
floatcoins[5] = {0.05, 0.1, 0.25, 0.5, 1.0};float *floatPtr; // Pointertoafloat
intcount; // array index
floatPtr = coins; // floatPtrnow points tocoins array
cout.precision(2);
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
23/51
Starting Out with C++, 3rdEdition
23
Program continues
for(count = 0; count < 5; count++)
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
24/51
Starting Out with C++, 3rdEdition
24
3URJUDP2XWSXW
Here are the values in the coins array:0.050.10.250.51
And here they are again:
0.050.10.250.51
8/6/2019 ITEI303- Lecture 2 Pointers in C++
25/51
Starting Out with C++, 3rdEdition
25
Program 9-8// This programuses theaddress ofeach element in
thearray.
#include
#include
void main(void)
{
floatcoins[5] = {0.05, 0.1, 0.25, 0.5, 1.0};
float *floatPtr; // Pointertoafloat
intcount; // array index
cout.precision(2);cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
26/51
Starting Out with C++, 3rdEdition
26
Program continues
for(count = 0; count < 5; count++)
{
floatPtr = &coins[count];
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
27/51
Starting Out with C++, 3rdEdition
27
3URJUDP2XWSXW
Here are the values in the coins array:
0.050.10.250.51
8/6/2019 ITEI303- Lecture 2 Pointers in C++
28/51
Starting Out with C++, 3rdEdition
28
9.4 Pointer Arithmetic
Some mathematical operations may beperformed on pointers.
The ++ and operators may be used toincrement or decrement a pointer variable.
An integer may be added to or subtracted froma pointer variable. This may be performed with
the +, - +=, or-= operators. A pointer may be subtracted from another
pointer.
8/6/2019 ITEI303- Lecture 2 Pointers in C++
29/51
Starting Out with C++, 3rdEdition
29
Program 9-9// This programuses a pointerto displaythecontents
// ofan integerarray.
#include
void main(void)
{
int set[8] = {5, 10, 15, 20, 25, 30, 35, 40};
int *nums, index;nums = set;
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
30/51
Starting Out with C++, 3rdEdition
30
Program continues
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
31/51
Starting Out with C++, 3rdEdition
31
3URJUDP2XWSXW
The numbers in set are:5101520253035 40The numbers in set backwards are:
403530252015105
8/6/2019 ITEI303- Lecture 2 Pointers in C++
32/51
Starting Out with C++, 3rdEdition
32
9.5 Initializing Pointers
Pointers may be initialized with the address
of an existing object.
8/6/2019 ITEI303- Lecture 2 Pointers in C++
33/51
Starting Out with C++, 3rdEdition
33
9.6 Comparing Pointers
If one address comes before another address
in memory, the first address is considered
less than the second. C++s relationaloperators maybe used to compare pointer
values.
8/6/2019 ITEI303- Lecture 2 Pointers in C++
34/51
Starting Out with C++, 3rdEdition
34
Figure 9-5
0x5A00
array[0] array[1] array[2] array[3] array[4]
0x5A04 0x5A08 0x5A0C 0x5A0F
(Addresses)
An array of five integers
8/6/2019 ITEI303- Lecture 2 Pointers in C++
35/51
Starting Out with C++, 3rdEdition
35
Program 9-10
// This programuses a pointerto displaythecontents
// ofan integerarray.
#include
void main(void)
{
int set[8] = {5, 10, 15, 20, 25, 30, 35, 40};
int *nums = set; // Makenums pointto set
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
36/51
Starting Out with C++, 3rdEdition
36
Program continues
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
37/51
Starting Out with C++, 3rdEdition
37
3URJUDP2XWSXW
The numbers in set are:5101520253035 40The numbers in set backwards are:
403530252015105
8/6/2019 ITEI303- Lecture 2 Pointers in C++
38/51
Starting Out with C++, 3rdEdition
38
9.7 Pointers as Function Parameters
A pointer can be used as a function
parameter. It gives the function access to
the original argument, much like a referenceparameter does.
8/6/2019 ITEI303- Lecture 2 Pointers in C++
39/51
Starting Out with C++, 3rdEdition
39
Program 9-11
// This programuses twofunctions thatacceptaddresses of
// variables as arguments.#include
// Function prototypes
void getNumber(int *);
void doubleValue(int *);
void main(void){
intnumber;
getNumber(&number) // Pass address ofnumbertogetNumber
doubleValue(&number); // and doubleValue.
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
40/51
Starting Out with C++, 3rdEdition
40
Program continues
// DefinitionofgetNumber. The parameter, Input, is a pointer.
// This functionasks theuserforanumber. The valueentered// is stored inthe variable pointed toby Input.
void getNumber(int *input)
{
cout > *input;
}
// Definitionof doubleValue. The parameter, val, is a pointer.
// This functionmultiplies the variable pointed toby valby
// two.
void doubleValue(int *val)
{
*val *= 2;
}
8/6/2019 ITEI303- Lecture 2 Pointers in C++
41/51
Starting Out with C++, 3rdEdition
41
3URJUDP2XWSXWZLWK([DPSOH,QSXW
Enter an integer number: >(QWHU@
That value doubled is 20
8/6/2019 ITEI303- Lecture 2 Pointers in C++
42/51
Starting Out with C++, 3rdEdition
42
Program 9-12
// This program demonstrates thata pointermaybeused as a// parametertoaccepttheaddress ofanarray. Either subscript
// or pointernotationmaybeused.
#include
#include
// Function prototypes
void getSales(float *);floattotalSales(float *);
void main(void)
{
float sales[4];
getSales(sales);cout.precision(2);
8/6/2019 ITEI303- Lecture 2 Pointers in C++
43/51
Starting Out with C++, 3rdEdition
43
Program continues
cout.setf(ios::fixed | ios::showpoint);
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
44/51
Starting Out with C++, 3rdEdition
44
Program continues
// DefinitionoftotalSales. This functionuses a pointerto
// accepttheaddress ofanarrayoffourfloats. Thefunction// gets thetotaloftheelements inthearrayand returns that
// value.(Pointernotation is used inthis function.)
floattotalSales(float *array)
{
float sum = 0.0;
for(intcount = 0; count < 4; count++)
{
sum += *array;
array++;
}return sum;
}
8/6/2019 ITEI303- Lecture 2 Pointers in C++
45/51
Starting Out with C++, 3rdEdition
45
3URJUDP2XWSXWZLWK([DPSOH,QSXW
Enter the sales figure for quarter1: >(QWHU@Enter the sales figure for quarter2: >(QWHU@Enter the sales figure for quarter3: >(QWHU@
Ente
r the
sale
s figure
for quarte
r 4:>(QWHU@
The total sales for the year are $48967.86
8/6/2019 ITEI303- Lecture 2 Pointers in C++
46/51
Starting Out with C++, 3rdEdition
46
9.8 Focus on Software Engineering:
Dynamic Memory Allocation
Variables may be created and destroyedwhile a program is running.
A pointer than contains the address 0 iscalled a null pointer.
Use the new operator to dynamicallyallocate memory.
Use delete to dynamically deallocatememory.
8/6/2019 ITEI303- Lecture 2 Pointers in C++
47/51
Starting Out with C++, 3rdEdition
47
Program 9-13// This programtotals and averages the sales figures forany
// numberof days. Thefigures are stored ina dynamically// allocated array.
#include
#include
void main(void)
{float *sales, total = 0, average;
intnumDays;
cout numDays;
sales = newfloat[numDays]; // Allocatememory
8/6/2019 ITEI303- Lecture 2 Pointers in C++
48/51
Starting Out with C++, 3rdEdition
48
Program continues
if(sales == NULL) // Testfornull pointer
{
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
49/51
Starting Out with C++, 3rdEdition
49
Program continues
// Calculatetheaverage sales per day
average = total / numDays;
// Displaytheresults
cout.precision(2);cout.setf(ios::fixed | ios::showpoint);
cout
8/6/2019 ITEI303- Lecture 2 Pointers in C++
50/51
Starting Out with C++, 3rdEdition
50
3URJUDP2XWSXWZLWK([DPSOH,QSXW
How many days of sales figures do you wish to process? >(QWHU@Enter the sales figures below.Day 1: >(QWHU@Day 2: >(QWHU@Day 3: >(QWHU@Day 4: >(QWHU@Day 5: >(QWHU@total sales: $4067.13
average sales: $813.43
8/6/2019 ITEI303- Lecture 2 Pointers in C++
51/51
St ti O t ith C++ 3rd Editi
51
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 thefunction as an argument.
A pointer to a dynamically allocated object.