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 * arg10      const char *ptr1 = nullptr; // initialize *ptr111

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.