1
Object-Oriented Programming
Using C++
A tutorial for pointers
2
Obj
ectiv
es •Read chapter 8
•Declare, initialize and use pointers
• Demonstrate the relationship between pointers and built-in arrays.
3
int main(){ int a; // a is an integer int *aPtr; // aPtr is a pointer to an integer a = 7; // assign the value 7 to the variable a aPtr = &a; // aPtr set to memory address // of the variable a cout << a; // displays the contents of variable a // displays a 7 cout << aPtr; // displays the contents of // variable aPtr which contains the // memory address of the variable a cout << *aPtr; // dereference operator * // says get the data that aPtr points to // displays a 7 (the contents of a)
4
// Cube a variable using call-by-value
#include <iostream>using namespace std;int cubeByValue( int ); // prototypeint main()
{int number = 5; cout << "The original value of number is” << number; number = cubeByValue( number ); cout << "\nThe new value of number is “ << number << endl;
return 0; }
5
int cubeByValue( int n )
{ // n is a local variable which is destroyed when the // the function ends
return n * n * n; // cube local variable n
}
6
// Cube a variable using call by // reference with a pointer argumentvoid cubeByReference( int * );
int main(){ int number = 5; cout << "The original value of number” << “ is " << number;
cubeByReference( &number );
cout << "\nThe new value of number is” << number << endl;
return 0;
}
7
// call to the function inside maincubeByReference( &number );
void cubeByReference( int *nPtr )
{
*nPtr = *nPtr * *nPtr * *nPtr;
}
number (at 3fe0)
5
nPtr
3fe0
8
// Attempting to modify a constant pointer to// constant data.
int main()
{ int x = 5, y; const int *const ptr = &x;
cout << *ptr << endl;
*ptr = 7;
ptr = &y;
return 0;
}
9
// Attempting to modify a constant pointer to// constant data.
int main()
{ int x = 5, y; const int *const ptr = &x;
cout << *ptr << endl;
*ptr = 7; // not legal because
ptr = &y; // not legal because
return 0;
}
10
// Relationship between pointers and built-in arrays
// Using subscripting and pointer// notations with arrays
int main()
{ int b[ ] = { 10, 20, 30, 40 }, i;
int *bPtr = b; // set bPtr to point to array b
for ( i = 0; i < 4; i++ )
cout << b[ i ] << '\n‘ // i equals 0 << *( b + i ) << '\n'; << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';
11
// Using subscripting and pointer// notations with arrays
int main()
{ int b[] = { 10, 20, 30, 40 }, i;
int *bPtr = b; // set bPtr to point to array b
for ( i = 0; i < 4; i++ )
cout << b[ i ] << '\n‘ << *( b + i ) << '\n'; // i equals 0 << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';
12
// Using subscripting and pointer// notations with arrays
int main()
{ int b[] = { 10, 20, 30, 40 }, i, offset;
int *bPtr = b; // set bPtr to point to array b
for ( i = 0; i < 4; i++ )
cout << b[ i ] << '\n‘ << *( b + i ) << '\n'; << bPtr[ i ] << '\n'; // i equals 0 << *( bPtr + i ) << '\n';
13
// Using subscripting and pointer// notations with arrays
int main()
{ int b[ ] = { 10, 20, 30, 40 }, i, offset;
int *bPtr = b; // set bPtr to point to array b
for ( i = 0; i < 4; i++ )
cout << b[ i ] << '\n‘ << *( b + i ) << '\n'; << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n'; // i equals 0
14
// Using subscripting and pointer// notations with arrays
int main()
{ int b[ ] = { 10, 20, 30, 40 }, i, offset;
int *bPtr = b; // set bPtr to point to array b
for ( i = 0; i < 4; i++ )
cout << b[ i ] << '\n‘ // i equals 1 << *( b + i ) << '\n'; << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';
15
// Using subscripting and pointer// notations with arrays
int main()
{ int b[] = { 10, 20, 30, 40 }, i, offset;
int *bPtr = b; // set bPtr to point to array b
for ( i = 0; i < 4; i++ )
cout << b[ i ] << '\n‘ << *( b + i ) << '\n'; // i equals 1 << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n';
16
// Using subscripting and pointer// notations with arrays
int main()
{ int b[] = { 10, 20, 30, 40 }, i, offset;
int *bPtr = b; // set bPtr to point to array b
for ( i = 0; i < 4; i++ )
cout << b[ i ] << '\n‘ << *( b + i ) << '\n'; << bPtr[ i ] << '\n'; // i equals 1 << *( bPtr + i ) << '\n';
17
// Using subscripting and pointer// notations with arrays
int main()
{ int b[ ] = { 10, 20, 30, 40 }, i, offset;
int *bPtr = b; // set bPtr to point to array b
for ( i = 0; i < 4; i++ )
cout << b[ i ] << '\n‘ << *( b + i ) << '\n'; << bPtr[ i ] << '\n'; << *( bPtr + i ) << '\n'; // i equals 1
18
// Copying a string using array notation// and pointer notation.#include <iostream>using namespace std;void copy1( char *, const char * );void copy2( char *, const char * );int main(){ char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy1( string1, string2 ); cout << "string1 = " << string1 << endl;
copy2( string3, string4 ); cout << "string3 = " << string3 << endl; return 0; }
19
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy1( string1, string2 ); // in main_________________________________
void copy1( char *s1, const char *s2 ){ for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) ; // do nothing in body}
20
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy1( string1, string2 ); // in main_________________________________
void copy1( char *s1, const char *s2 ){ for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) ; // do nothing in body}
H e l l o \0
21
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy1( string1, string2 ); // in main_________________________________
void copy1( char *s1, const char *s2 ){ for ( int i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) ; // do nothing in body}
H e l l o \0
// when i equals 5
22
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G
23
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G o
24
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G o o
25
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G o o d
26
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G o o d
27
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G o o d B
28
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G o o d B y
29
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G o o d B y e
30
// in main_________________________________
char string1[ 10 ], *string2 = "Hello", string3[ 10 ], string4[ ] = "Good Bye";
copy2( string3, string4 ); // in main_________________________________
// copy s2 to s1 using pointer notationvoid copy2( char *s1, const char *s2 ){ for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) ; // do nothing in body}
G o o d B y e \0
31
const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
suit[0] suit[1] suit[2] suit[3]
sizeof (suit) is 16 if pointers are 4 bytes 8 if pointers are 2 bytes
An array of pointers to stringsEach element of the array contains the address of the first character in each string
32
const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
suit[0] suit[1] suit[2] suit[3]
cout << suit[0];
cout << (suit[0] + 2);
cout << *(suit[3] + 3);
cout << *(suit + 2);
Heartsarts
d
Clubs // same as suit[2]
33
String compare:
char *s1 = “Happy New Year”;char *s2 = “Happy New Year”;
char *s3 = “Happy Holidays”;
if (s1 = = s2)
cout << “strings are equal”;
else
cout << “strings are not equal”;
34
String compare:
char *s1 = “Happy New Year”;char *s2 = “Happy New Year”;
char *s3 = “Happy Holidays”;
if (s1 = = s2)
cout << “strings are equal”;
else
cout << “strings are not equal”; // why?
35
char *s1 = “Happy New Year”;char *s2 = “Happy New Year”;
char *s3 = “Happy Holidays”;
if (strcmp(s1,s2) == 0)
cout << “strings are equal”;
else
cout << “strings are not equal”;
String compare using strcmp within# include <cstring>
36
String compare using strcmp within# include <cstring> char *s1 = “Happy New Year”;char *s2 = “Happy New Year”;
char *s3 = “Happy Holidays”;if (strcmp(s1,s3 ) == 0) cout << “strings are equal”;else
if ((strcmp s1,s3 ) < 0) cout << “string1 is less than string3); else
cout << “string1 is greater than string3”;
37
String compare:warning:
strcmp returns a negative value if string1 is less than string2 (not a –1)strcmp returns a positive value if string1 is greater than string2 (not a +1)strcmp returns a 0 if the strings are equal
38
Do self-review exercises 8.1, 8.2, 8.3,8.4 pages 364-365
Check your answers on page 366