![]()
// ex05_21.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void mystery1( char *, const char * );
int main()
{
char string1[ 80 ], string2[ 80 ];
cout << "Enter two strings:
";
cin >> string1 >>
string2;
mystery1( string1, string2 );
cout << string1 << endl;
return 0;
}
void mystery1( char *s1, const char *s2 )
{
while ( *s1 != '\0' )
++s1;
for ( ; *s1 = *s2; s1++, s2++ )
; //
empty
statement
}
![]()
// ex05_22.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int mystery2( const char * );
int main()
{
char string[ 80 ];
cout << "Enter a string: ";
cin >> string;
cout << mystery2( string )
<<
endl;
return 0;
}
int mystery2( const char *s )
{
int x;
for ( x = 0; *s != '\0'; s++ )
++x;
return x;
}
![]()
// ex05_30.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
bool mystery3( const char *, const char * );
int main()
{
char string1[ 80 ], string2[ 80 ];
cout << "Enter two strings:
";
cin >> string1 >>
string2;
cout << "The result is "
<< mystery3(
string1, string2 ) << endl;
return 0;
}
bool mystery3( const char *s1, const char *s2 )
{
for ( ; *s1 != '\0' && *s2
!= '\0';
s1++, s2++ )
if ( *s1 != *s2
)
return
false;
return true;
}
![]()
// Fig. 5.4: fig05_04.cpp
// Using the & and * operators
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int
a;
// a is an integer
int *aPtr; // aPtr
is
a pointer to an integer
a = 7;
aPtr = &a; //
aPtr
set to address of a
cout << "The address of a
is " <<
&a
<< "\nThe
value of aPtr is " << aPtr;
cout << "\n\nThe value of a
is " <<
a
<< "\nThe
value of *aPtr is " << *aPtr;
cout << "\n\nShowing that *
and &
are inverses of "
<< "each
other.\n&*aPtr = " << &*aPtr
<< "\n*&aPtr
= " << *&aPtr << endl;
return 0;
}
![]()
// Fig. 5.6: fig05_06.cpp
// Cube a variable using call-by-value
#include <iostream>
using std::cout;
using std::endl;
int cubeByValue( int ); // prototype
int 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;
}
int cubeByValue( int n )
{
return n * n * n; //
cube local
variable n
}
![]()
// Fig. 5.7: fig05_07.cpp
// Cube a variable using call-by-reference
// with a pointer argument
#include <iostream>
using std::cout;
using std::endl;
void cubeByReference( int * ); // prototype
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;
}
void cubeByReference( int *nPtr )
{
*nPtr = *nPtr * *nPtr * *nPtr;
//
cube number in main
}
![]()
// Fig. 5.10: fig05_10.cpp
// Converting lowercase letters to uppercase
letters
// using a non-constant pointer to non-constant
data
#include <iostream>
using std::cout;
using std::endl;
#include <cctype>
void convertToUppercase( char * );
int main()
{
char string[] = "characters and
$32.98";
cout << "The string before
conversion
is: " << string;
convertToUppercase( string );
cout << "\nThe string after
conversion
is: "
<< string
<< endl;
return 0;
}
void convertToUppercase( char *sPtr )
{
while ( *sPtr != '\0' ) {
if ( islower(
*sPtr ) )
*sPtr
= toupper( *sPtr ); // convert to uppercase
++sPtr;
// move sPtr
to the next character
}
}
![]()
// Fig. 5.11: fig05_11.cpp
// Printing a string one character at a time using
// a non-constant pointer to constant data
#include <iostream>
using std::cout;
using std::endl;
void printCharacters( const char * );
int main()
{
char string[] = "print characters of
a string";
cout << "The string is:\n";
printCharacters( string );
cout << endl;
return 0;
}
// In printCharacters, sPtr cannot modify the
character
// to which it points. sPtr is a "read-only"
pointer
void printCharacters( const char *sPtr )
{
for ( ; *sPtr != '\0'; sPtr++
)
// no initialization
cout <<
*sPtr;
}
![]()
// Fig. 5.12: fig05_12.cpp
// Attempting to modify data through a
// non-constant pointer to constant data.
#include <iostream>
void f( const int * );
int main()
{
int y;
f( &y ); // f attempts illegal modification
return 0;
}
// xPtr cannot modify the value of the variable
// to which it points
void f( const int *xPtr )
{
*xPtr = 100; // cannot modify
a const
object
}
![]()
// Fig. 5.13: fig05_13.cpp
// Attempting to modify a constant pointer to
// non-constant data
#include <iostream>
int main()
{
int x, y;
int * const ptr = &x; // ptr
is a constant
pointer to an
// integer. An integer can be modified
// through ptr, but ptr always points
// to the same memory location.
*ptr = 7;
ptr = &y;
return 0;
}
![]()
// Fig. 5.14: fig05_14.cpp
// Attempting to modify a constant pointer to
// constant data.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int x = 5, y;
const int *const ptr = &x; //
ptr is
a constant pointer to a
// constant integer. ptr always
// points to the same location
// and the integer at that
// location cannot be modified.
cout << *ptr << endl;
*ptr = 7;
ptr = &y;
return 0;
}
![]()
// Fig. 5.15: fig05_15.cpp
// This program puts values into an array, sorts
the
values into
// ascending order, and prints the resulting
array.
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
void bubbleSort( int *, const int );
int main()
{
const int arraySize = 10;
int a[ arraySize ] = { 2, 6, 4, 8,
10, 12,
89, 68, 45, 37 };
int i;
cout << "Data items in original order\n";
for ( i = 0; i < arraySize;
i++ )
cout <<
setw( 4
) << a[ i ];
bubbleSort( a, arraySize
);
// sort the array
cout << "\nData items in
ascending
order\n";
for ( i = 0; i < arraySize;
i++ )
cout <<
setw( 4
) << a[ i ];
cout << endl;
return 0;
}
void bubbleSort( int *array, const int size )
{
void swap( int * const, int * const
);
for ( int pass = 0; pass < size - 1; pass++ )
for ( int j = 0; j < size - 1; j++ )
if (
array[ j ] > array[ j + 1 ] )
swap( &array[ j ], &array[ j + 1 ] );
}
void swap( int * const element1Ptr, int *
const element2Ptr
)
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
![]()
// Fig. 5.16: fig05_16.cpp
// Sizeof operator when used on an array name
// returns the number of bytes in the array.
#include <iostream>
using std::cout;
using std::endl;
size_t getSize( double * );
int main()
{
double array[ 20 ];
cout << "The number of
bytes in the
array is "
<< sizeof(
array )
<< "\nThe
number of bytes returned by getSize is "
<< getSize(
array ) << endl;
return 0;
}
size_t getSize( double *ptr )
{
return sizeof( ptr );
}
![]()
// Fig. 5.17: fig05_17.cpp
// Demonstrating the sizeof operator
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
int main()
{
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
int array[ 20 ], *ptr = array;
cout << "sizeof c = "
<< sizeof
c
<< "\tsizeof(char)
= " << sizeof( char )
<< "\nsizeof
s = " << sizeof s
<< "\tsizeof(short)
= " << sizeof( short )
<< "\nsizeof
i = " << sizeof i
<< "\tsizeof(int)
= " << sizeof( int )
<< "\nsizeof
l = " << sizeof l
<< "\tsizeof(long)
= " << sizeof( long )
<< "\nsizeof
f = " << sizeof f
<< "\tsizeof(float)
= " << sizeof( float )
<< "\nsizeof
d = " << sizeof d
<< "\tsizeof(double)
= " << sizeof( double )
<< "\nsizeof
ld = " << sizeof ld
<< "\tsizeof(long
double) = " << sizeof( long double )
<< "\nsizeof
array = " << sizeof array
<< "\nsizeof
ptr = " << sizeof ptr
<< endl;
return 0;
}
![]()
// Fig. 5.20: fig05_20.cpp
// Using subscripting and pointer notations with
arrays
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int b[] = { 10, 20, 30, 40 }, i,
offset;
int *bPtr = b; // set
bPtr to
point to array b
cout << "Array b printed
with:\n"
<< "Array
subscript notation\n";
for ( i = 0; i < 4; i++ )
cout << "b["
<<
i << "] = " << b[ i ] << '\n';
cout << "\nPointer/offset
notation
where\n"
<< "the
pointer is the array name\n";
for ( offset = 0; offset < 4;
offset++
)
cout << "*(b
+ "
<< offset << ") = "
<< *( b + offset ) << '\n';
cout << "\nPointer subscript notation\n";
for ( i = 0; i < 4; i++ )
cout <<
"bPtr["
<< i << "] = " << bPtr[ i ] << '\n';
cout << "\nPointer/offset notation\n";
for ( offset = 0; offset < 4;
offset++
)
cout <<
"*(bPtr
+ " << offset << ") = "
<< *( bPtr + offset ) << '\n';
return 0;
}
![]()
// Fig. 5.21: fig05_21.cpp
// Copying a string using array notation
// and pointer notation.
#include <iostream>
using std::cout;
using std::endl;
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;
}
// copy s2 to s1 using array notation
void copy1( char *s1, const char *s2 )
{
for ( int i = 0; ( s1[ i ] = s2[ i ]
) !=
'\0'; i++ )
; //
do nothing
in body
}
// copy s2 to s1 using pointer notation
void copy2( char *s1, const char *s2 )
{
for ( ; ( *s1 = *s2 ) != '\0'; s1++,
s2++
)
; //
do nothing
in body
}
![]()
// Fig. 5.24: fig05_24.cpp
// Card shuffling dealing program
#include <iostream>
using std::cout;
#include <iomanip>
using std::ios;
using std::setw;
using std::setiosflags;
#include <cstdlib>
#include <ctime>
void shuffle( int [][ 13 ] );
void deal( const int [][ 13 ], const char *[],
const
char *[] );
int main()
{
const char *suit[ 4 ] =
{ "Hearts",
"Diamonds",
"Clubs", "Spades" };
const char *face[ 13 ] =
{ "Ace", "Deuce",
"Three",
"Four",
"Five", "Six",
"Seven", "Eight",
"Nine", "Ten",
"Jack", "Queen", "King" };
int deck[ 4 ][ 13 ] = { 0 };
srand( time( 0 ) );
shuffle( deck );
deal( deck, face, suit );
return 0;
}
void shuffle( int wDeck[][ 13 ] )
{
int row, column;
for ( int card = 1; card <=
52; card++
) {
do {
row
= rand() % 4;
column
= rand() % 13;
} while( wDeck[
row ][
column ] != 0 );
wDeck[ row ][
column ]
= card;
}
}
void deal( const int wDeck[][ 13 ], const char
*wFace[],
const char *wSuit[] )
{
for ( int card = 1; card <= 52;
card++
)
for ( int row = 0; row <= 3; row++ )
for ( int column = 0; column <= 12; column++ )
if ( wDeck[ row ][ column ] == card )
cout << setw( 5 ) << setiosflags( ios::right )
<< wFace[ column ] << " of "
<< setw( 8 ) << setiosflags( ios::left )
<< wSuit[ row ]
<< ( card % 2 == 0 ? '\n' : '\t' );
}
![]()
// Fig. 5.26: fig05_26.cpp
// Multipurpose sorting program using function
pointers
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
void bubble( int [], const int, bool (*)( int,
int ) );
bool ascending( int, int );
bool descending( int, int );
int main()
{
const int arraySize = 10;
int order,
counter,
a[ arraySize
] =
{ 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
cout << "Enter 1 to sort in
ascending
order,\n"
<< "Enter
2 to sort in descending order: ";
cin >> order;
cout << "\nData items in
original
order\n";
for ( counter = 0; counter <
arraySize;
counter++ )
cout <<
setw( 4
) << a[ counter ];
if ( order == 1 ) {
bubble( a,
arraySize,
ascending );
cout <<
"\nData
items in ascending order\n";
}
else {
bubble( a,
arraySize,
descending );
cout <<
"\nData
items in descending order\n";
}
for ( counter = 0; counter <
arraySize;
counter++ )
cout <<
setw( 4
) << a[ counter ];
cout << endl;
return 0;
}
void bubble( int work[], const int size,
bool (*compare)( int, int ) )
{
void swap( int * const, int * const
);
// prototype
for ( int pass = 1; pass < size; pass++ )
for ( int count = 0; count < size - 1; count++ )
if (
(*compare)( work[ count ], work[ count + 1 ] ) )
swap( &work[ count ], &work[ count + 1 ] );
}
void swap( int * const element1Ptr, int *
const element2Ptr
)
{
int temp;
temp = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = temp;
}
bool ascending( int a, int b )
{
return b < a; // swap
if
b is less than a
}
bool descending( int a, int b )
{
return b > a; // swap
if b is
greater than a
}
![]()
// Fig. 5.28: fig05_28.cpp
// Demonstrating an array of pointers to functions
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void function1( int );
void function2( int );
void function3( int );
int main()
{
void (*f[ 3 ])( int ) = { function1,
function2,
function3 };
int choice;
cout << "Enter a number
between 0 and
2, 3 to end: ";
cin >> choice;
while ( choice >= 0 &&
choice <
3 ) {
(*f[ choice ])(
choice
);
cout <<
"Enter a
number between 0 and 2, 3 to end: ";
cin >>
choice;
}
cout << "Program execution
completed."
<< endl;
return 0;
}
void function1( int a )
{
cout << "You entered "
<< a
<< "
so function1 was called\n\n";
}
void function2( int b )
{
cout << "You entered "
<< b
<< "
so function2 was called\n\n";
}
void function3( int c )
{
cout << "You entered "
<< c
<< "
so function3 was called\n\n";
}
![]()
// Fig. 5.30: fig05_30.cpp
// Using strcpy and strncpy
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char x[] = "Happy Birthday to You";
char y[ 25 ], z[ 15 ];
cout << "The string in
array x is:
" << x
<< "\nThe
string in array y is: " << strcpy( y, x )
<< '\n';
strncpy( z, x, 14 ); // does
not copy
null character
z[ 14 ] = '\0';
cout << "The string in array z
is:
" << z << endl;
return 0;
}
![]()
// Fig. 5.31: fig05_31.cpp
// Using strcat and strncat
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char s1[ 20 ] = "Happy ";
char s2[] = "New Year ";
char s3[ 40 ] = "";
cout << "s1 = " << s1
<<
"\ns2 = " << s2;
cout << "\nstrcat(s1, s2) = "
<<
strcat( s1, s2 );
cout << "\nstrncat(s3, s1, 6)
= "
<< strncat( s3, s1, 6 );
cout << "\nstrcat(s3, s1) = "
<<
strcat( s3, s1 ) << endl;
return 0;
}
![]()
// Fig. 5.32: fig05_32.cpp
// Using strcmp and strncmp
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstring>
int main()
{
char *s1 = "Happy New Year";
char *s2 = "Happy New Year";
char *s3 = "Happy Holidays";
cout << "s1 = " << s1
<<
"\ns2 = " << s2
<< "\ns3
= " << s3 << "\n\nstrcmp(s1, s2) = "
<< setw(
2 ) << strcmp( s1, s2 )
<< "\nstrcmp(s1,
s3) = " << setw( 2 )
<< strcmp(
s1, s3 ) << "\nstrcmp(s3, s1) = "
<< setw(
2 ) << strcmp( s3, s1 );
cout << "\n\nstrncmp(s1,
s3, 6) = "
<< setw( 2 )
<< strncmp(
s1, s3, 6 ) << "\nstrncmp(s1, s3, 7) = "
<< setw(
2 ) << strncmp( s1, s3, 7 )
<< "\nstrncmp(s3,
s1, 7) = "
<< setw(
2 ) << strncmp( s3, s1, 7 ) << endl;
return 0;
}
![]()
// Fig. 5.33: fig05_33.cpp
// Using strtok
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char string[] = "This is a sentence
with
7 tokens";
char *tokenPtr;
cout << "The string to be
tokenized
is:\n" << string
<< "\n\nThe
tokens are:\n";
tokenPtr = strtok( string, " " );
while ( tokenPtr != NULL ) {
cout <<
tokenPtr
<< '\n';
tokenPtr = strtok(
NULL,
" " );
}
return 0;
}
![]()
// Fig. 5.34: fig05_34.cpp
// Using strlen
#include <iostream>
using std::cout;
using std::endl;
#include <cstring>
int main()
{
char *string1 =
"abcdefghijklmnopqrstuvwxyz";
char *string2 = "four";
char *string3 = "Boston";
cout << "The length of \""
<<
string1
<< "\"
is " << strlen( string1 )
<< "\nThe
length of \"" << string2
<< "\"
is " << strlen( string2 )
<< "\nThe
length of \"" << string3
<< "\"
is " << strlen( string3 ) << endl;
return 0;
}