19.10. Conversion to Pointer-Based char * Strings

You can convert string class objects to 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 19.9 demonstrates conversion of strings to pointer-based strings.


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


string string1 is STRINGS
string1 converted to a pointer-based string is STRINGS
ptr1 is STRINGS
ptr2 is STRINGS


Fig. 19.9. Converting strings to pointer-based strings and character arrays.

The program declares a string, a size_t and two char pointers (lines 9–12). The string string1 is initialized to "STRINGS", ptr1 is initialized to nullptr 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 15 uses string member function copy to copy object string1 into the char array pointed to by ptr2. Line 16 places a terminating null character in the array pointed to by ptr2.

Line 20 uses function c_str to obtain a const char * that points to a null terminated pointer-based string with the same content as string1. The pointer is passed to the stream insertion operator for output.

Line 26 assigns the const char * ptr1 a pointer returned by class string member function data. This member function returns a non-null-terminated built-in character array. 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 invalid—which could lead to unpredictable results.

Lines 29–30 use pointer arithmetic to output the character array pointed to by ptr1. In lines 32–33, the pointer-based string ptr2 is output and the memory allocated for ptr2 is deleted to avoid a memory leak.


Image Common Programming Error 19.2

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


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.196.121