It returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
DeclarationFollowing is the declaration for std::string::c_str.
const char* c_str() const;C++11
const char* c_str() const noexcept;C++14
const char* c_str() const noexcept;Parameters
none
Return ValueIt returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
Exceptionsif an exception is thrown, there are no changes in the string.
ExampleIn below example for std::string::c_str.
#include <iostream> #include <cstring> #include <string> int main () { std::string str ("Please divide this sentance into parts"); char * cstr = new char [str.length()+1]; std::strcpy (cstr, str.c_str()); char * p = std::strtok (cstr," "); while (p!=0) { std::cout << p << '\n'; p = std::strtok(NULL," "); } delete[] cstr; return 0; }
The sample output should be like this −
Please divide this sentance into parts
string.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4