Chapter 10 ABSOLUTE C++ Pointers and Dynamic Arrays WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 10 Pointers and Dynamic Arrays
Learning Objectives ◆Pointers ◆Pointer variables ◆Memory management ◆Dynamic Arrays ◆Creating and using ◆Pointer arithmetic Classes,Pointers,Dynamic Arrays ◆The this pointer Destructors,copy constructors Copyright 2006 Pearson Addison-Wesley.All rights reserved. 10-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-2 Learning Objectives ¨ Pointers ¨ Pointer variables ¨ Memory management ¨ Dynamic Arrays ¨ Creating and using ¨ Pointer arithmetic ¨ Classes, Pointers, Dynamic Arrays ¨ The this pointer ¨ Destructors, copy constructors
Pointer Introduction ◆Pointer definition: Memory address of a variable Recall:memory divided +Numbered memory locations Addresses used as name for variable ◆You've used pointers already! Call-by-reference parameters +Address of actual argument was passed Copyright 2006 Pearson Addison-Wesley.All rights reserved. 10-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-3 Pointer Introduction ¨ Pointer definition: ¨Memory address of a variable ¨ Recall: memory divided ¨Numbered memory locations ¨Addresses used as name for variable ¨ You’ve used pointers already! ¨Call-by-reference parameters ¨Address of actual argument was passed
Pointer Variables Pointers are "typed" Can store pointer in variable ◆Not int,double,etc. Instead:A POINTER to int,double,etc.! ◆Example: double *p; p is declared a "pointer to double"variable Can hold pointers to variables of type double ◆Not other types! Copyright 2006 Pearson Addison-Wesley.All rights reserved. 10-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-4 Pointer Variables ¨ Pointers are "typed" ¨ Can store pointer in variable ¨ Not int, double, etc. ¨ Instead: A POINTER to int, double, etc.! ¨ Example: double *p; ¨ p is declared a "pointer to double" variable ¨ Can hold pointers to variables of type double ¨ Not other types!
Declaring Pointer Variables Pointers declared like other types Add "*before variable name Produces "pointer to"that type "*must be before each variable ◆int*p1,*p2,v1,v2; p1,p2 hold pointers to int variables v1,v2 are ordinary int variables Copyright006 Pearson Addison-Wesley.All rights reserved. 10-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 10-5 Declaring Pointer Variables ¨ Pointers declared like other types ¨Add "*" before variable name ¨Produces "pointer to" that type ¨ "*" must be before each variable ¨ int *p1, *p2, v1, v2; ¨p1, p2 hold pointers to int variables ¨v1, v2 are ordinary int variables