'\0' in a C++ string?


R

Ray Mitchell

Hello,

My understanding is that C++ string class objects still terminate their
strings with a '\0', as do strings in C. If this is the case (please tell
me if it isn't), it appears that the easiest way to treat a C++ string as a
char * so it can be used with functions requiring a character pointer such
as strcmp (I realize that one would not normally want to do this), etc. is
to use the form: &stringObject[0] (is there a better way?). In MFC, one
need to merely cast the CString type to a char *, but this doesn't appear to
be the case with a standard C++ string.

Thanks,
Ray Mitchell
 
Ad

Advertisements

J

Jacques Labuschagne

Ray said:
Hello,

My understanding is that C++ string class objects still terminate their
strings with a '\0', as do strings in C. If this is the case (please tell
me if it isn't), it appears that the easiest way to treat a C++ string as a

This is only the case when you get the string using the c_str member
function.
char * so it can be used with functions requiring a character pointer such
as strcmp (I realize that one would not normally want to do this), etc. is
to use the form: &stringObject[0] (is there a better way?). In MFC, one

No! String elements are not guarenteed contiguous. Use c_str(), that's
what it's there for.
need to merely cast the CString type to a char *, but this doesn't appear to
be the case with a standard C++ string.

It isn't. Also note that it's perfectly legal for a std::string to have
multiple '\0' characters inside. The string isn't just chopped off at
the first '\0'.

Jacques.
 
R

Rolf Magnus

Ray said:
Hello,

My understanding is that C++ string class objects still terminate
their strings with a '\0', as do strings in C.

They might do this internally, or they might not.
If this is the case (please tell me if it isn't), it appears that the
easiest way to treat a C++ string as a char * so it can be used with
functions requiring a character pointer such as strcmp (I realize that
one would not normally want to do this), etc. is to use the form:
&stringObject[0] (is there a better way?).

This is not guaranteed to work. If you need a C style string, use the
member function c_str().
In MFC, one need to merely cast the CString type to a char *, but this
doesn't appear to be the case with a standard C++ string.

That's right. It would be dangerous to support this, therefore it's not
allowed.
 
D

Daniel T.

Ray Mitchell said:
Hello,

My understanding is that C++ string class objects still terminate their
strings with a '\0', as do strings in C.

No they do not:

string s( "abc\000def" );
assert( s.size() == 7 );
 
Ad

Advertisements

D

Daniel T.

Ray Mitchell said:
My understanding is that C++ string class objects still terminate their
strings with a '\0', as do strings in C.

Let me try that again.

int main() {
string s;
for ( int x = 0; x < 100; ++x )
s.push_back( '\0' );
assert( s.size() == 100 );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top