Skip to main content

std::string c_str() method

// Const version only
constexpr const CharT* c_str() const noexcept;

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.

Invalidation

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

Undefined Behavior

Writing to the character array accessed through c_str is undefined behavior

.

Parameters

(none)

Return value

A pointer to the underlying character storage.

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

Complexity

Constant - O(1).

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.

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

Example

Main.cpp
#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>

extern "C" {
void c_func(const char* c_str) { printf("c_func called with '%s'\n", c_str); }
}

int main() {
std::string const s("Emplary");
const char* p = s.c_str();
assert(s.size() == std::strlen(p));
assert(std::equal(s.begin(), s.end(), p));
assert(std::equal(p, p + s.size(), s.begin()));
assert('\0' == *(p + s.size()));

c_func(s.c_str());
}
}
Output
c_func called with 'Emplary'
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string c_str() method

// Const version only
constexpr const CharT* c_str() const noexcept;

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.

Invalidation

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

Undefined Behavior

Writing to the character array accessed through c_str is undefined behavior

.

Parameters

(none)

Return value

A pointer to the underlying character storage.

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

Complexity

Constant - O(1).

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.

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

Example

Main.cpp
#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>

extern "C" {
void c_func(const char* c_str) { printf("c_func called with '%s'\n", c_str); }
}

int main() {
std::string const s("Emplary");
const char* p = s.c_str();
assert(s.size() == std::strlen(p));
assert(std::equal(s.begin(), s.end(), p));
assert(std::equal(p, p + s.size(), s.begin()));
assert('\0' == *(p + s.size()));

c_func(s.c_str());
}
}
Output
c_func called with 'Emplary'
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.