CHAP15 - 1 Chapter 15 Pointers, Dynamic Data, and Reference...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 15 Pointers, Dynamic Data, and Reference Types Dale/Weems/Headington 2 Chapter 15 Topics ● Using the Address-Of Operator & ● Declaring and Using Pointer Variables ● Using the Indirection (Dereference) Operator * ● The NULL Pointer ● Using C++ Operators new and delete ● Meaning of an Inaccessible Object ● Meaning of a Dangling Pointer ● Use of a Class Destructor ● Shallow Copy vs. Deep Copy of Class Objects ● Use of a Copy Constructor 3 Recall that . . . char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant because the value of str itself cannot be changed by assignment. It “points” to the memory location of a char . str [0] [1] [2] [3] [4] [5] [6] [7] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ 6000 4 Addresses in Memory ● when a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the address of the variable int x; float number; char ch; 2000 2002 2006 x number ch 5 Obtaining Memory Addresses ● the address of a non-array variable can be obtained by using the address-of operator & int x; float number; char ch; cout << “Address of x is “ << &x << endl; cout << “Address of number is “ << &number << endl; cout << “Address of ch is “ << &ch << endl; 6 What is a pointer variable? ● A pointer variable is a variable whose value is the address of a location in memory . ● to declare a pointer variable, you must specify the type of value that the pointer will point to,for example, int* ptr; // ptr will hold the address of an int char* q; // q will hold the address of a char 7 Using a Pointer Variable int x; x = 12; int* ptr; ptr = &x; NOTE: Because ptr holds the address of x, we say that ptr “points to” x 2000 12 x 3000 2000 ptr 8 2000 12 x 3000 2000 ptr int x; x = 12; int* ptr; ptr = &x; cout << *ptr; NOTE: The value pointed to by ptr is denoted by *ptr Unary operator * is the indirection (deference) operator 9 int x; x = 12; int* ptr; ptr = &x; *ptr = 5; // changes the value // at address ptr to 5 Using the Dereference Operator 2000 12 5 x 3000 2000 ptr 10 char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; p = q; // the rhs has value 4000 // now p and q both point to ch Another Example 4000 A Z ch 5000 6000 4000 4000 q p 11 ptr Using a Pointer to Access the Elements of a String ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ 3000 char msg[ ] = “Hello”; ‘M’ ‘a’ 3001 char* ptr; 3000 ptr = msg; // recall that msg == &msg[ 0 ] *ptr = ‘M’ ; ptr++; // increments the address in ptr *ptr = ‘a’; 12 int StringLength ( /* in */ const char str[ ] ) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Precondition: str is a null-terminated string // Postcondition: FCTVAL == length of str (not counting ‘\0’) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -...
View Full Document

This note was uploaded on 06/13/2011 for the course CSC 240 taught by Professor Lebre during the Spring '04 term at Moraine Valley Community College.

Page1 / 65

CHAP15 - 1 Chapter 15 Pointers, Dynamic Data, and Reference...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online