Conversion to C-Style Pointer-Based char * Strings

Class string provides member functions for converting string class objects to C-style pointer-based strings. As mentioned earlier, unlike pointer-based strings, strings are not necessarily null terminated. These conversion functions are useful when a given function takes a pointer-based string as an argument. Figure 18.9 demonstrates conversion of strings to pointer-based strings.

Figure 18.9. Converting strings to C-style strings and character arrays.

(This item is displayed on page 900 in the print version)

1 // Fig. 18.9: Fig18_09.cpp 2 // Converting to C-style strings. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include 8 using std::string; 9 10 int main() 11 { 12 string string1( "STRINGS" ); // string constructor with char* arg 13 const char *ptr1 = 0; // initialize *ptr1 14 int length = string1.length(); 15 char *ptr2 = new char[ length + 1 ]; // including null 16 17 // copy characters from string1 into allocated memory 18 string1.copy( ptr2, length, 0 ); // copy string1 to ptr2 char* 19 ptr2[ length ] = ''; // add null terminator 20 21 cout << "string string1 is " << string1 22 << " string1 converted to a C-Style string is " 23 << string1.c_str() << " ptr1 is "; 24 25 // Assign to pointer ptr1 the const char * returned by 26 // function data(). NOTE: this is a potentially dangerous 27 // assignment. If string1 is modified, pointer ptr1 can 28 // become invalid. 29 ptr1 = string1.data(); 30 31 // output each character using pointer 32 for ( int i = 0; i < length; i++ ) 33 cout << *( ptr1 + i ); // use pointer arithmetic 34 35 cout << " ptr2 is " << ptr2 << endl; 36 delete [] ptr2; // reclaim dynamically allocated memory 37 return 0; 38 } // end main  

string string1 is STRINGS string1 converted to a C-Style string is STRINGS ptr1 is STRINGS ptr2 is STRINGS  

The program declares a string, an int and two char pointers (lines 1215). The string string1 is initialized to "STRINGS", ptr1 is initialized to 0 and length is initialized to the length of string1. Memory of sufficient size to hold a pointer-based string equivalent of string string1 is allocated dynamically and attached to char pointer ptr2.

Line 18 uses string member function copy to copy object string1 into the char array pointed to by ptr2. Line 19 manually places a terminating null character in the array pointed to by ptr2.

Line 23 uses function c_str to copy object string1 and automatically add a terminating null character. This function returns a const char * which is output by the stream insertion operator.

Line 29 assigns the const char * ptr1 a pointer returned by class string member function data. This member function returns a non-null-terminated C-style character array. Note that we do not modify string string1 in this example. If string1 were to be modified (e.g., the string's dynamic memory changes its address due to a member function call such as string1.insert( 0, "abcd" );), ptr1 could become invalidwhich could lead to unpredictable results.


Lines 3233 use pointer arithmetic to output the character array pointed to by ptr1. In lines 3536, the C-style string pointed to by ptr2 is output and the memory allocated for ptr2 is deleted to avoid a memory leak.


Common Programming Error 18.4

Not terminating the character array returned by data with a null character can lead to execution-time errors.

Good Programming Practice 18.1

Whenever possible, use the more robust string class objects rather than C-style pointer-based strings.

Категории