17

Possible Duplicate:
string c_str() vs. data()

I use strncpy(dest, src_string, 32) to convert std::string to char[32] to make my C++ classes work with legacy C code. But does std::string's c_str() method always return a null-terminated string?

Community
  • 1
  • 1
ezpresso
  • 7,896
  • 13
  • 62
  • 94
  • 3
    yes, but you may not be copying it over if your `strncpy` is too small. Say your null terminator is at index 40 and you only copy 32 over. Not null terminated anymore. – im so confused Oct 02 '12 at 20:31
  • 1
    `c_str()` returns C string, defined by C standard as null-terminated. I call this not a real question, because all we are to do here is to explain what "C string" means, which doesn't really fit on this site. – Griwes Oct 02 '12 at 20:34
  • @Griwes I don't think this should be closed as NARQ, RTFM would be a more appropriate reason to close :) – Praetorian Oct 02 '12 at 20:38

3 Answers3

29

Does std::string's c_str() method always return a null-terminated string?

Yes.

It's specification is:

Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].

Note that the range specified for i is closed, so that size() is a valid index, referring to the character past the end of the string.

operator[] is specified thus:

Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object of type T with value charT()

In the case of std::string, which is an alias for std::basic_string<char> so that charT is char, a value-constructed char has the value zero; therefore the character array pointed to by the result of std::string::c_str() is zero-terminated.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    It seems worth mentioning that there may also be embedded NULs. That is, it is quite possible to construct a std::string that contains NUL characters, simply by doing str.push_back(0);. This is an advantage of using str::string, but something that should be kept in mind. Then again, it is unusual for an std::string to contain an embedded NUL and in many uses of std::string such a thing will never happen - they aren't going to magically appear. So, just be aware of it. – Bruce Dawson Aug 24 '17 at 21:14
12

c_str returns a "C string". And C strings are always terminated by a null character. This is C standard.

Null terminating strings.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
2

According to this, the answer is yes.

fbrereto
  • 35,429
  • 19
  • 126
  • 178
  • 1
    whoa, i use that site all the time, why is it bad?? – im so confused Oct 02 '12 at 20:32
  • 8
    @nightcracker: The vulgar language is unnecessary, as is the criticism. Yeah, cplusplus.com has (had?) errors and 'meh' examples, but for whatever reason that's avalanched into "oh this is my chance to hate something beyond what's reasonable criticism". Let's cut it out. I personally go there from time to time for a quick reference, via search engine. Also, not every answer needs a standard quote. – GManNickG Oct 02 '12 at 20:34
  • 1
    @Griwes: Indeed; just like any publication in the history of publishing, it's possible to find a handful of errors or poor choices of wording if you look hard enough. All the important ones in that particular rant have been fixed long ago. – Mike Seymour Oct 02 '12 at 20:43
  • 1
    @MikeSeymour, not really: `using namespace std;`... don't want to check the others. – Griwes Oct 02 '12 at 20:45
  • 6
    @Griwes: Whether or not it's appropriate to use that to make example code (or even production code, as long as you leave it out of headers) easier to read is entirely subjective, as are most of the criticisms. The site is a reference to how C++ works, not how you or anyone else likes to write it. – Mike Seymour Oct 02 '12 at 20:49
  • 1
    @MikeSeymour, nah, I forgot that SO doesn't care about good style, only about "it works = it's good", my bad. – Griwes Oct 02 '12 at 21:04
  • 2
    @Griwes: To the contrary, the people on SO get much more leeway to care *more* about good style because we get to say "if you were to approach the solution without constraint...", ignoring any more practical constraints on the problem. That said, it's still important to understand we are often *engineers*, who, at the end of the day, need to solve a problem. – GManNickG Oct 02 '12 at 21:10
  • 1
    @Griwes: SO doesn't care about anything. Many people here (including myself) do care about good style, but not about pointless rules like the ones your link claims should be upheld at all times; especially since blindly following rules often leads to very bad style. – Mike Seymour Oct 02 '12 at 21:12