290 likes | 496 Views
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. Introduction.
E N D
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
Introduction • Pointers • Powerful, but difficult to master • Simulate pass-by-reference • Close relationship with arrays and strings
count countPtr count 7 7 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 (indirect reference) • Indirection • Referencing value through pointer • Pointer declarations • * indicates variable is pointer int *countPtr; declares pointer to int, pointer of type int * • Multiple pointers require multiple asterisks int *myPtr1, *myPtr2;
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
yptr y y 5 yPtr 600004 600000 600000 5 address of y is value of yptr Pointer Operators • & (address operator) • Returns memory address of its operand • Example int y = 5;int *yPtr;yPtr = &y; // yPtr gets address of y • yPtr “points to” y
Pointer Operators • * (indirection/dereferencing operator) • Returns synonym for object its pointer operand points to • *yPtr returns y (because yPtr points to y). • dereferenced pointer is lvalue *yptr = 9; // assigns 9 to y • * and & are inverses of each other • Example 1 #include <iostream> 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; }
Pointer of complex structure #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; } • Example 2
Other Pointer Operations #include <iostream> int main() { int i=3, j=3, *p1, *p2; p1=&i, p2=&j; if(*p1==*p2) //used to check if the content is the same. cout <<"*p1 Equal to *p2"<<endl; else cout<<"*p1 not Equal to *p2"<<endl; if(p1==p2) //used to check if they are pointing to the same unit. cout <<"p1 Equal to p2"<<endl; else cout <<"p1 not Equal to p2"<<endl; if(p1<=p2) //it has side-effect, do not use it. cout <<"p1 no more than p2"<<endl; else cout <<"p1 less than p2"<<endl; return 0; } • Example 3
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
Calling Functions by Reference #include <iostream> void f_point( int *i, int *j) { *i=3; *j=4; } void f_reference( int &i, int &j) { i=3; j=4; } void f_value (int i, int j) { i=3; j=4; } int main() { int p=1, q=2; f_point(&p,&q); cout << "p is "<< p << " q is " <<q <<endl; p=1; q=2; f_reference(p,q); cout << "p is "<< p << " q is " <<q <<endl; p=1; q=2; f_value(p,q); cout << "p is "<< p << " q is " <<q <<endl; return 0; }
Relationship Between Pointers and Arrays • Arrays and pointers closely related • Array name like constant pointer • Pointers can do array subscripting operations
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 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 Last element Relationship Between Pointers and Arrays *(pointer+0), or *p *(pointer+7)
Relationship Between Pointers and Arrays • Accessing array elements with pointers • Element b[ n ] can be accessed by *( bPtr + n ) • Called pointer/offset notation • Addresses • &b[ 3 ] same as bPtr + 3 • Array name can be treated as pointer • b[ 3 ] same as *( b + 3 ) • Pointers can be subscripted (pointer/subscript notation) • bPtr[ 3 ] same as b[ 3 ]
Relationship Between Pointers and Arrays • Example 5 #include <iostream> void f_point( char * i, char * j) { *(i+2)='E'; *(j+3)='E'; } void f_array( char i[], char j[]) { i[2]='E'; j[3]='E'; } int main() { char p[6]="hello", q[10]="howareyou"; f_point(p,q); cout << "p is "<< p << " q is " <<q <<endl; strcpy(p,"hello"); strcpy(q,"howareyou"); f_array(p,q); cout << "p is "<< p << " q is " <<q <<endl; return 0; }
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 *timePtr = new time( 12, 0, 0 ); • Allocating arrays • int *gradesArray = new int[ 10 ]; • Example 6
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 [] gradesArray; • First calls destructor for each object in array • Then deallocates memory • delete time;
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+1]; cin >> p; cout <<"p is: " <<p; delete [] p; p=NULL; return 0; }
Dynamic Data Storage • Example 7: Multiple-phase development • Step 1: 7_1.cc • Step 2: 7_2.cc • Step 3: 7_3.cc
Dynamic Data Storage MyArray::MyArray() { size=0; ptr=NULL; } MyArray::MyArray(int t_size) { if(t_size>0){ size=t_size; ptr=new int[t_size+1]; cout << "Please input "<<t_size<< " intergers:"; for(int i=0;i<t_size;i++) cin >> ptr[i]; } else{ size=0; ptr=NULL; } } MyArray::~MyArray() { size=0; delete [] ptr; } • 7_3.cc #include <iostream> class MyArray{ public: MyArray(); MyArray(int); ~MyArray(); void printall(); void array_copy(MyArray&); private: int size; int *ptr; };
Dynamic Data Storage void MyArray::printall() { if(size==0) { cout <<"NULL"<<endl; return; } for(int i=0;i<size;i++) cout<<ptr[i]<<" "; cout <<endl; } void MyArray::array_copy(MyArray & b) { size=b.size; delete [] ptr; ptr=new int [size+1]; for(int i=0; i<size;i++) ptr[i]=b.ptr[i]; ptr[size]=0; } • 7_3.cc (continue) int main() { MyArray 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; }