std::basic_string::c_str(3)
π std::basic_string::c_str(3) π
okay bestie, welcome to the C++ Standard Libary page for std::basic_string::c_str β yes, that manual name is spelled exactly like that up top, and we are respecting the source material. β¨ This is the whole page, every tiny detail included, because we don't do "summary" here. π
πΈ NAME πΈ
std::basic_string::c_str - std::basic_string::c_str
self-titled queen behavior, honestly π
π Synopsis π
const CharT* c_str() const; (noexcept since C++11)
(constexpr since C++20)
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.
Your pointer 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()andrend()(since C++11).
β οΈ Writing to the character array accessed through c_str() is undefined behavior. That is a hard no, bestie. Do not touch. π
Also: c_str() and data() perform the same function. (since C++11) β periodt. π
π Parameters π
(none) β literally no parameters. She takes nothing. Iconic. π
π¦ Return value π¦
Pointer to the underlying character storage. β¨
- until C++11:
c_str()[i] == operator[](i)for everyiin[0, size()). - since C++11:
c_str() + i == std::addressof(operator[](i))for everyiin[0, size()].
π Complexity π
Constant. β‘
π 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. π«Ά
π₯ Example π₯
The page gives us // Run this code as the cue, so here is the code, exactly:
#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'
π See also π
frontβ accesses the first character.(DR*)(public member function)backβ accesses the last character.(DR*)(public member function)dataβ returns a pointer to the first character of a string.(public member function)
made with π from http://cppreference.com, dated 2024.06.10, for std::basic_string::c_str(3). that's the whole page, bestie β slay and stay null-terminated. πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms