다음을 통해 공유


basic_string::c_str

Converts the contents of a string as a C-style, null-terminated string.

const value_type *c_str( ) const;

반환 값

A pointer to the C-style version of the invoking string. The pointer value is not valid after calling a non-const function, including the destructor, in the basic_string class on the object.

설명

Objects of type string belonging to the C++ template class basic_string<char> are not necessarily null terminated. The null character ' \0 ' is used as a special character in a C-string to mark the end of the string but has no special meaning in an object of type string and may be a part of the string just like any other character. There is an automatic conversion from const char* into strings, but the string class does not provide for automatic conversions from C-style strings to objects of type basic_string<char>.

The returned C-style string should not be modified, as this could invalidate the pointer to the string, or deleted, as the string has a limited lifetime and is owned by the class string.

예제

// basic_string_c_str.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;

   string  str1 ( "Hello world" );
   cout << "The original string object str1 is: " 
        << str1 << endl;
   cout << "The length of the string object str1 = " 
        << str1.length ( ) << endl << endl;

   // Converting a string to an array of characters
   const char *ptr1 = 0;
   ptr1= str1.data ( );
   cout << "The modified string object ptr1 is: " << ptr1 
        << endl;
   cout << "The length of character array str1 = " 
        << strlen ( ptr1) << endl << endl;

   // Converting a string to a C-style string
   const char *c_str1 = str1.c_str ( );
   cout << "The C-style string c_str1 is: " << c_str1 
        << endl;
   cout << "The length of C-style string str1 = " 
        << strlen ( c_str1) << endl << endl;
}
  

요구 사항

헤더: <string>

네임스페이스: std

참고 항목

참조

basic_string 클래스