missing zeroterminator in string.c_str()?

K

klaus hoffmann

In the following program I would expect r to be a zero terminated string of
length 1. Am I wrong? The STL I'm using omit the zeroterm in this situation.
Klaus

#include <iostream>

using namespace std; //introduces namespace std

int main(){
string x("12");
string::iterator p(x.begin()),q(x.begin()+1);
string y(p,q);
const char *r=y.c_str();
cout <<r << " "<< int(r[0])<< " "<< int(r[1]);
return 0;
end;
 
V

Victor Bazarov

klaus hoffmann said:
In the following program I would expect r to be a zero terminated string of
length 1. Am I wrong?

The program is not supposed to compile. 'string' is undefined.
Once you #include <string>, it works fine for me. 'y' is a string
of length 1, and it should only contain '1'. 'c_str()' for it
returns a string that contains '1' and '\0'.
The STL I'm using omit the zeroterm in this situation.

Chuck it. Get a decent implementation.
Klaus

#include <iostream>

using namespace std; //introduces namespace std

int main(){
string x("12");
string::iterator p(x.begin()),q(x.begin()+1);
string y(p,q);
const char *r=y.c_str();
cout <<r << " "<< int(r[0])<< " "<< int(r[1]);
return 0;
end;

"end;" ? What language is your native? :)

Victor
 
K

klaus hoffmann

[snip]

Thank you Victor, thank you Adam,

sorry for posting invalid code. Of course <string> was missing and the IDE seems
to have dropped the error msg somehow (problem with precompiled headers).
I had cut the (IMO) relevant part and somehow put in that "end;" (I used Pascal
in a former life).

sincerely
Klaus
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top