std::basic_string::c_str(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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() and rend() (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 every i in [0, size()).
  • since C++11: c_str() + i == std::addressof(operator[](i)) for every i in [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