1 Chapter 5 Pointers and Strings Outline Introduction





























- Slides: 29
1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Pointer of Complex Structure Other Pointer Operations Calling Functions by Reference Relationship between pointer and array Dynamic Data Storage 2003 Prentice Hall, Inc. All rights reserved.
2 Introduction • Pointers – Powerful, but difficult to master – Simulate pass-by-reference – Close relationship with arrays and strings 2003 Prentice Hall, Inc. All rights reserved.
3 Pointer Variable Declarations and Initialization • Pointer variables – Contain memory addresses as values – Normally, variable contains specific value (direct reference) – Pointers contain address of variable that has specific value count. Ptr (indirect reference) • Indirection 7 – Referencing value through pointer • Pointer declarations – * indicates variable is pointer int *count. Ptr; declares pointer to int, pointer of type int * – Multiple pointers require multiple asterisks int *my. Ptr 1, *my. Ptr 2; 2003 Prentice Hall, Inc. All rights reserved. count 7
Pointer Variable Declarations and Initialization • Can declare pointers to any data type • Pointer initialization – Initialized to 0, NULL, or address • 0 or NULL points to nothing 2003 Prentice Hall, Inc. All rights reserved. 4
5 Pointer Operators • & (address operator) – Returns memory address of its operand – Example int y = 5; int *y. Ptr; y. Ptr = &y; // y. Ptr gets address of y – y. Ptr “points to” y y. Ptr y 5 yptr 600004 600000 y 600000 address of y is value of yptr 2003 Prentice Hall, Inc. All rights reserved. 5
6 Pointer Operators • * (indirection/dereferencing operator) – Returns synonym for object its pointer operand points to – *y. Ptr returns y (because y. Ptr points to y). – dereferenced pointer is lvalue *yptr = 9; // assigns 9 to y • * and & are inverses of each other #include <iostream> • Example 1 int main() { int *p; int q=3, k=5; p=&q; cout << "p is " <<p<<" *p is " <<*p<<" q is "<<q<<endl; *p=4; cout << "p is " <<p<<" *p is " <<*p<<" q is "<<q<<endl; *p=k; cout << "p is " <<p<<" *p is " <<*p<<" q is "<<q<<endl; return 0; 2003 Prentice Hall, Inc. All rights reserved. }
7 Pointer Operators 2003 Prentice Hall, Inc. All rights reserved.
8 Pointer of complex structure • Example 2 #include <iostream> struct Student{ int grade; char name[20]; }; int main() { struct Student *p, q; q. grade = 90; strcpy(q. name, "Smith"); cout << "Grade " <<q. grade<< " Name " <<q. name<<endl; p=&q; cout << "Grade " << p->grade <<" Name "<<p->name<<endl; return 0; } 2003 Prentice Hall, Inc. All rights reserved.
9 Pointer of complex structure 2003 Prentice Hall, Inc. All rights reserved.
10 Other Pointer Operations #include <iostream> • Example 3 int main() { int i=3, j=3, *p 1, *p 2; p 1=&i, p 2=&j; if(*p 1==*p 2) //used to check if the content is the same. cout <<"*p 1 Equal to *p 2"<<endl; else cout<<"*p 1 not Equal to *p 2"<<endl; if(p 1==p 2) //used to check if they are pointing to the same unit. cout <<"p 1 Equal to p 2"<<endl; else cout <<"p 1 not Equal to p 2"<<endl; if(p 1<=p 2) //it has side-effect, do not use it. cout <<"p 1 no more than p 2"<<endl; else cout <<"p 1 less than p 2"<<endl; return 0; } 2003 Prentice Hall, Inc. All rights reserved.
11 Other Pointer Operations 2003 Prentice Hall, Inc. All rights reserved.
12 Calling Functions by Reference • 3 ways to pass arguments to function – Pass-by-value arguments – Pass-by-reference with reference arguments – Pass-by-pointer arguments • return can return one value from function • Example 4 2003 Prentice Hall, Inc. All rights reserved.
13 Calling Functions by Reference #include <iostream> int main() { void f_point( int *i, int *j) int p=1, q=2; { *i=3; f_point(&p, &q); *j=4; cout << "p is "<< p << " q is " <<q <<endl; } p=1; void f_reference( int &i, int &j) q=2; { f_reference(p, q); i=3; cout << "p is "<< p << " q is " <<q <<endl; j=4; } p=1; q=2; void f_value (int i, int j) f_value(p, q); { cout << "p is "<< p << " q is " <<q <<endl; i=3; j=4; return 0; } } 2003 Prentice Hall, Inc. All rights reserved.
14 Calling Functions by Reference 2003 Prentice Hall, Inc. All rights reserved.
15 Relationship Between Pointers and Arrays • Arrays and pointers closely related – Array name like constant pointer – Pointers can do array subscripting operations 2003 Prentice Hall, Inc. All rights reserved.
16 Relationship Between Pointers and Arrays • Pointer variable and name of array Pointer pointing to the address of c[0] Name of array (Note that all elements of this array starts from here, c) c[0] -45 c[1] 6 c[2] 0 c[3] 72 c[4] 1543 c[5] -89 c[6] 0 c[7] 62 c[8] -3 c[9] 1 c[10] 6453 c[11] 78 Position number of the element within array c 2003 Prentice Hall, Inc. All rights reserved.
17 Relationship Between Pointers and Arrays Name of array (Note that all elements of this array starts from here, c) *(pointer+0), or *p *(pointer+7) c[0] -45 c[1] 6 c[2] 0 c[3] 72 c[4] 1543 c[5] -89 c[6] 0 c[7] 62 c[8] -3 c[9] 1 c[10] 6453 c[11] 78 Last element 2003 Prentice Hall, Inc. All rights reserved.
18 Relationship Between Pointers and Arrays • Accessing array elements with pointers – Element b[ n ] can be accessed by *( b. Ptr + n ) • Called pointer/offset notation – Addresses • &b[ 3 ] same as b. Ptr + 3 – Array name can be treated as pointer • b[ 3 ] same as *( b + 3 ) – Pointers can be subscripted (pointer/subscript notation) • b. Ptr[ 3 ] same as b[ 3 ] 2003 Prentice Hall, Inc. All rights reserved.
19 Relationship Between Pointers and Arrays • Example 5 #include <iostream> int main() { void f_point( char * i, char * j) char p[6]="hello", q[10]="howareyou"; { *(i+2)='E'; f_point(p, q); *(j+3)='E'; cout << "p is "<< p << " q is " <<q <<endl; } strcpy(p, "hello"); void f_array( char i[], char j[]) strcpy(q, "howareyou"); { f_array(p, q); i[2]='E'; cout << "p is "<< p << " q is " <<q <<endl; j[3]='E'; } return 0; } 2003 Prentice Hall, Inc. All rights reserved.
20 Dynamic Data Storage • Dynamic memory management – Control allocation and deallocation of memory – Operators new and delete • new operator – – Create memory for object Calls default constructor for object Returns pointer of specified type Format • Providing initializers double *ptr = new double( 3. 14159 ); Time *time. Ptr = new time( 12, 0, 0 ); • Allocating arrays int *grades. Array = new int[ 10 ]; 2003 Prentice Hall, Inc. All rights reserved.
21 Dynamic Data Storage • delete – Destroy dynamically allocated object and free space – Operator delete • Calls destructor for object • Deallocates memory associated with object – Memory can be reused to allocate other objects – Deallocating arrays • delete [] grades. Array; – First calls destructor for each object in array – Then deallocates memory • delete time; 2003 Prentice Hall, Inc. All rights reserved.
22 Dynamic Data Storage • Example 6 #include <iostream> #include <string> int main() { char * p; int index; cout << "Input how many characters: " ; cin >>index; p = new char [index]; cin >> p; cout <<"p is: " <<p; delete [] p; p=NULL; return 0; } 2003 Prentice Hall, Inc. All rights reserved.
23 Dynamic Data Storage 2003 Prentice Hall, Inc. All rights reserved.
24 Dynamic Data Storage • Example 7: Coding in C++ int main() { My. Array a, b(4); cout << "Before copying"<<endl; a. printall(); b. printall(); a. array_copy(b); cout << "After copying"<<endl; a. printall(); b. printall(); return 0; } 2003 Prentice Hall, Inc. All rights reserved.
25 Dynamic Data Storage int main() { return 0; } 2003 Prentice Hall, Inc. All rights reserved.
26 Dynamic Data Storage • Multiple-phase development – Step 1: 7_1. cc – Step 2: 7_2. cc – Step 3: 7_3. cc 2003 Prentice Hall, Inc. All rights reserved.
27 Dynamic Data Storage My. Array: : My. Array() My. Array: : ~My. Array() • 7_3. cc { { size=0; #include <iostream> ptr=NULL; delete [] ptr; using namespace std; } } class My. Array{ My. Array: : My. Array(int t_size) public: { My. Array(); if(t_size>0){ My. Array(int); size=t_size; ~My. Array(); ptr=new int[t_size]; void printall(); cout << "Please input "<<t_size<< " intergers: "; void array_copy(My. Array&); for(int i=0; i<t_size; i++) cin >> ptr[i]; private: } int size; else{ int *ptr; size=0; }; ptr=NULL; } } 2003 Prentice Hall, Inc. All rights reserved.
28 Dynamic Data Storage void My. Array: : printall() { • 7_3. cc (continue) if(size==0) { cout <<"NULL"<<endl; return; int main() } { for(int i=0; i<size; i++) My. Array a, b(4); cout<<ptr[i]<<" "; cout << "Before copying"<<endl; cout <<endl; a. printall(); } b. printall(); void My. Array: : array_copy(My. Array & b) a. array_copy(b); { cout << "After copying"<<endl; size=b. size; a. printall(); delete [] ptr; b. printall(); ptr=new int [size]; return 0; for(int i=0; i<size; i++) } ptr[i]=b. ptr[i]; } 2003 Prentice Hall, Inc. All rights reserved.
29 Dynamic Data Storage 2003 Prentice Hall, Inc. All rights reserved.