By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

'\0' in a C++ string?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ray Mitchell wrote:
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.
Jul 22 '05 #2

P: n/a
Ray Mitchell wrote:
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.

Jul 22 '05 #3

P: n/a
In article <Uv******************@newsread1.news.pas.earthlink .net>,
"Ray Mitchell" <Ra*********@MeanOldTeacher.com> wrote:
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 );
Jul 22 '05 #4

P: n/a
In article <Uv******************@newsread1.news.pas.earthlink .net>,
"Ray Mitchell" <Ra*********@MeanOldTeacher.com> wrote:
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 );
}
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.