{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
1 Chapter 15 Pointers, Dynamic Data, and Reference Types Dale/Weems/Headington
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full Document Right Arrow Icon
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
Background image of page 4
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;
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full Document Right Arrow Icon
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
Background image of page 8
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
Background image of page 9

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

View Full Document Right Arrow Icon
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
Background image of page 10
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’;
Background image of page 11

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

View Full Document Right Arrow Icon
12 int StringLength ( /* in */ const char str[ ] ) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Precondition: str is a null-terminated string // Postcondition: FCTVAL == length of str (not counting ‘\0’) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { char* p ; int count = 0; p = str; while ( *p != ‘\0’ ) { count++ ; p++ ; // increments the address p by sizeof char } return count; }
Background image of page 12
13 C++ Data Types C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double
Background image of page 13

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

View Full Document Right Arrow Icon
Some C++ Pointer Operations Precedence Higher -> Select member of class pointed to Unary: ++ -- ! * new delete Increment, Decrement, NOT, Dereference, Allocate, Deallocate + - Add Subtract < <= > >= Relational operators == != Tests for equality, inequality Lower = Assignment
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}