0% found this document useful (0 votes)
11 views15 pages

CSCE 1001 Chapter 9

cs

Uploaded by

abdelrahmansendo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

CSCE 1001 Chapter 9

cs

Uploaded by

abdelrahmansendo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter 9

Pointers and Dynamic Arrays

Copyright © 2014 Pearson Addison-Wesley. All rights reserved.


9.1
Pointers

Copyright © 2014 Pearson Addison-Wesley. All rights reserved.


Pointers

 A pointer is the memory address of a variable


 Memory addresses can be used as names for
variables
 If a variable is stored in three memory

locations, the address of the first can be used


as a name for the variable.
 When a variable is used as a call-by-reference

argument, its address is passed

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 3


Pointers Tell
Where To Find A Variable

 An address used to tell where a variable is stored


in memory is a pointer

 Pointers "point" to a variable by telling where


the variable is located

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 4


Declaring Pointers

 Pointer variables must be declared to have a


pointer type
 Example: To declare a pointer variable p that

can "point" to a variable of type double:

double *p;
 The asterisk identifies p as a pointer variable

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 5


Multiple Pointer Declarations

 To declare multiple pointers in a statement, use


the asterisk before each pointer variable
 Example:

int *p1, *p2, v1, v2;

p1 and p2 point to variables of type int


v1 and v2 are variables of type int

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 6


The address of Operator

 The & operator can be used to determine the


address of a variable which can be assigned to a
pointer variable
 Example: p1 = &v1;

p1 is now a pointer to v1 (an alias)


v1 can be called v1 or "the variable pointed to
by p1"

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 7


The Dereferencing Operator

 C++ uses the * operator in yet another way with


pointers
 The phrase "The variable pointed to by p" is

translated into C++ as *p


 Here the * is the dereferencing operator

 p is said to be dereferenced

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 8


A Pointer Example

 v1 = 0; v1 and *p1 now refer to


p1 = &v1; the same variable
*p1 = 42;
cout << v1 << endl;
cout << *p1 << endl;

output:
42
42

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 9


Pointer Assignment
 The assignment operator = is used to assign
the value of one pointer to another
 Example: If p1 still points to v1 (previous slide)
then
p2 = p1;

causes *p2, *p1, and v1 all to name


the same variable

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 10


Caution! Pointer Assignments
 Some care is required making assignments to
pointer variables
 p1= p3; // changes the location that p1 "points" to

 *p1 = *p3; // changes the value at the location that


// p1 "points" to

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 11


Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 1- 12
The new Operator

 Using pointers, variables can be manipulated


even if there is no identifier for them
 To create a pointer to a new "nameless"

variable of type int:


p1 = new int;
 The new variable is referred to as *p1

 *p1 can be used anyplace an integer variable

can
cin >> *p1;
*p1 = *p1 + 7;
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 13
Basic Memory Management

 An area of memory called the freestore or the


heap is reserved for dynamic variables
 New dynamic variables use memory in the

freestore
 If all of the freestore is used, calls to new will

fail
 Unneeded memory can be recycled
 When variables are no longer needed, they

can be deleted and the memory they used is


returned to the freestore
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 14
The delete Operator

 When dynamic variables are no longer needed,


delete them to return memory to the freestore
 Example:

delete p;

The value of p is now undefined and the


memory used by the variable that p pointed to
is back in the freestore

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Slide 9- 15

You might also like