I have read several topics here and on other resources and did not found exact answer. The answer seems to be never. Please confirm.
Stop. Not never. As noted in Why can you assign nullptr to std::string?:
std::string str {nullptr}; // Undefined behavior
So if this will not throw (depends on implementation), c_str()
could return (char_t*)nullptr
.
I believe there are no other ways to get nullptr
from c_str()
. But must ask StackOverflow community to be sure. Thanks for attention.
No. Since c_str
returns a pointer p
to a null-terminated array of characters, there must be some value i >= 0
such that p[i] == '\0'
, and thus p
cannot be null.