280 likes | 446 Vues
Chapter 5. Pointers and Strings. Objectives. Ability to use pointers Ability to use pointers to functions call by reference Understand the relationship among pointers, arrays and strings Understand use of pointers to function Ability to declare and use arrays of strings.
E N D
Chapter 5 Pointers and Strings
Objectives • Ability to use pointers • Ability to use pointers to functions call by reference • Understand the relationship among pointers, arrays and strings • Understand use of pointers to function • Ability to declare and use arrays of strings
Pointer Declaration and Initialization Declaration ---------------------------------- int *myIntPtr, myInt = 6; Initialization -------------------------------- myIntPtr = &myInt; Usage ------------------------------------------- cout << “The Value of My Int is “<< *myIntPtr; Output -------------------------------------------- The Value of My Int is 6
The * operator • indirection or dereferencing • displays the value of the object the pointer is referencing • Format in which a pointer is output is machine dependant
Three ways to pass arguments to a function • Call by value • Call by reference with reference arguments • Call by reference using pointer arguments
Call by reference with pointers • Called function must define a pointer parameter to receive the address • void square( int *myIntPtr ) • Function prototype • void square(int *) • Function call • square( &myInt);
Passing Arrays to Functions • Compiler does not differentiate between single subscripted array and a pointer • compiler converts the function parameter to a const pointer • int b[] and int * const b are equivalent
Good Practice • Use Call by Value unless the caller explicitly requires that the called function modify the value of the argument in the callers environment
Using Const with Pointers const qualifier informs the compiler not to modify the variable If a value does not change in the body of a function to which it is passed the parameter should be declared const to ensure that it is not accidentally modified.
Using Const with Pointers const pointers to non const data ensure that the location pointed to does not change. Only one value can be modified in the calling function when call by value is used. If more parms need to be modified then they need to be passed call by reference.
// Fig. 5.10: fig05_10.cpp // Converting lowercase letters to uppercase letters // using a non-constant pointer to non-constant data #include <iostream.h> #include <ctype.h> 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 ( *sPtr >= 'a' && *sPtr <= 'z' ) *sPtr = toupper( *sPtr ); // convert to uppercase ++sPtr; // move sPtr to the next character } }
// Fig. 5.16: fig05_16.cpp // Sizeof operator when used on an array name // returns the number of bytes in the array. #include <iostream.h> size_t getSize( float * ); int main() { float 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( float *ptr ) { return sizeof( ptr ); }
Pointer Expressions and Pointer Arithmetic • Pointer may be • incremented ++ • decremented -- • an integer may be added to it + or += • or subtracted - or -= • or one pointer may be subtracted from another • Pointer Arithmetic is Machine Dependant
Pointer Expressions and Pointer Arithmetic • If int vPtr == 3000 • vPtr += 2 does not equal 3002 • vPtr will equal 3000 + 2*sizeof(int pointer) • vPtr ++ will move one integer pointer value forward
Pointer Expressions and Pointer Arithmetic • Pointers can be assigned to another pointer if both pointers are of the same type • A cast can be used to convert • void * is a generic pointer - no cast required • void * cannot be dereferenced
Relationships between Pointers and Arrays int b[5], *bPtr; // b is the address of the first element of b or &b[0] bPtr = b; // We do not need to get the address of b[] // b[3] can also be accessed as *(bPtr + 3) using pointer offset notation
// Fig. 5.21: fig05_21.cpp // Copying a string using array notation // and pointer notation. #include <iostream.h> 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 }
Arrays of Pointers • String Array • char *suit[4] = {“Hearts”,”Diamonds”,”Clubs”,”Spades”}; • Character Array • char *suit[4] [9]= {“Hearts”,”Diamonds”,”Clubs”,”Spades”};
Function Pointers • A function pointer is a pointer to the address of the function in memory • Pointers to functions can be passed to functions, returned, stored in arrays and assigned to other function pointers • ex 5.26
Characters and Strings String Declarations char color[] = “blue”; char *color = blue; These are equivalent - WHY? Accepting Strings from Display char line[80]; cin.getline (sentence, 80, ‘\n’);
String Manipulation stddef.h string.h strcpy() strncpy() strcat() strncat() strcmp() strncmp() strtok() strlen()
// Fig. 5.33: fig05_33.cpp // Using strtok #include <iostream.h> #include <string.h> 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.h> #include <string.h> 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; }