string::data() mem function

W

wenmang

Hi,
I am wondering whether string class's member function data() returns a
string of chars without '\0'?
Thanks.
 
P

peter.koch.larsen

(e-mail address removed) skrev:
Hi,
I am wondering whether string class's member function data() returns a
string of chars without '\0'?
Thanks.

Yes it does. Although you in practical use often will be able to use
data() as a const string (because the implementation might well append
a zero for different reasons), you should use c_str() whenever you want
the C-style string.

/Peter
 
A

Alf P. Steinbach

* (e-mail address removed):
I am wondering whether string class's member function data() returns a
string of chars without '\0'?

How did you start wondering about that, I wonder?

Anyway, check your documentation (you might also look five threads up in
this group).

Or, as they said in the old days, RTFM.
 
V

Victor Bazarov

I am wondering whether string class's member function data() returns a
string of chars without '\0'?

'std::string::data()' returns the contents of the string as the string
has them, \0 _can_ be part of it if you say so:

std::string s(10, '\0'); // s has 10 chars, all '\0'

V
 
W

wenmang

Actually, I am looking for sting::toByte() function, unfortunately, it
is not there for C++. I don't want to include the null byte into the
final buffer which will be TCPed to the other node.
 
J

Jim Langston

Actually, I am looking for sting::toByte() function, unfortunately, it
is not there for C++. I don't want to include the null byte into the
final buffer which will be TCPed to the other node.

Umm... since you're going off the raw data, and aren't needing (nor wanting)
the null terminator, you obvoiusly then are needing to go off the length of
the data. Since you have a pointer to the beginning of the data, and the
length, what does it matter in the least if there is a null terminator or
not since you're just not going to use it? In fact, I don't believe that
you could be guaranteed that it's not null terminated, since it's past your
end of data it could contain anything (undefined) and might in fact contain
a null byte.

In other words: don't worry about it. Use .data() and .length() and don't
care if it's null terminated.
 

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

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top