chapter 5: pointers and strings
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
2003 Prentice Hall, Inc. All rights reserved.
1
Chapter 5: Pointers and Strings
OutlineIntroduction
Pointer Variable Declarations and Initialization Pointer Operators
Pointer of Complex StructureOther Pointer OperationsCalling Functions by ReferenceRelationship between pointer and arrayDynamic 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
(indirect reference)• Indirection
– Referencing value through pointer• Pointer declarations
– * indicates variable is pointerint *countPtr;
declares pointer to int, pointer of type int *– Multiple pointers require multiple asterisks
int *myPtr1, *myPtr2;
count
7
countPtr
count
7
2003 Prentice Hall, Inc. All rights reserved.
4
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.
5
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
yPtr
y5
yptr
600004 600000
y
600000 5
address of y is value of yptr
2003 Prentice Hall, Inc. All rights reserved.
6
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;
}
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
• Example 3
#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;
}
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>
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;
}
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
• Pointer variable and name of array
c[6]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
Name of array (Note that all elements of this array starts from here, c)
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Position number of the element within array c
Pointer pointing to the address of c[0]
Relationship Between Pointers and Arrays
2003 Prentice Hall, Inc. All rights reserved.
17
c[6]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
Name of array (Note that all elements of this array starts from here, c)
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Last element
*(pointer+0), or *p
*(pointer+7)
Relationship Between Pointers and Arrays
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 *( 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 ]
2003 Prentice Hall, Inc. All rights reserved.
19
#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;
}
Relationship Between Pointers and Arrays
•Example 5
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 initializersdouble *ptr = new double( 3.14159 );Time *timePtr = new time( 12, 0, 0 );• Allocating arraysint *gradesArray = new int[ 10 ];
• Example 6
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 [] gradesArray;– 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+1];
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: 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.
25
Dynamic Data Storage
• 7_3.cc#include <iostream>
class MyArray{
public:
MyArray();
MyArray(int);
~MyArray();
void printall();
void array_copy(MyArray&);
private:
int size;
int *ptr;
};
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;
}
2003 Prentice Hall, Inc. All rights reserved.
26
Dynamic Data Storage• 7_3.cc (continue)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;
}
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;
}
2003 Prentice Hall, Inc. All rights reserved.
27
Dynamic Data Storage