Namespaces
Variants
Actions

std::basic_string<CharT,Traits,Allocator>::c_str

From cppreference.com
 
 
 
std::basic_string
Member functions
Element access
Iterators
Capacity
Modifiers
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 

Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
const CharT* c_str() const;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Returns a pointer to a null-terminated character array with data equivalent to those stored in the string.

The pointer is such that the range [c_str(); c_str() + size()] is valid and the values in it correspond to the values stored in the string with an additional null character after the last position.

The pointer obtained from c_str() may be invalidated by:

  • Passing a non-const reference to the string to any standard library function, or
  • Calling non-const member functions on the string, excluding operator[], at, front, back, begin, rbegin, end, and rend.

Writing to the character array accessed through c_str() is undefined behavior.

Since C++11, c_str() and data() perform the same function.

Contents

Parameters

(none)

Return value

Pointer to the underlying character storage.

data()[i] == operator[](i) for every i in [0, size()).

untilc++11

data() + i == &operator[](i) for every i in [0, size()].

sincec++11

Complexity

Constant.

Exceptions

noexcept specification:  
noexcept
  

Notes

The pointer obtained from c_str() may only be treated as a pointer to a null-terminated character string if the string object does not contain other null characters.

See also

Template:cpp/string/basic string/dcl list frontTemplate:cpp/string/basic string/dcl list backTemplate:cpp/string/basic string/dcl list data