Connecting Tech Pros Worldwide Forums | Help | Site Map

missing zeroterminator in string.c_str()?

klaus hoffmann
Guest
 
Posts: n/a
#1: Jul 19 '05
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;
Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 19 '05

re: missing zeroterminator in string.c_str()?


"klaus hoffmann" <dr.hoffmann.k@t-online.de> wrote...[color=blue]
> In the following program I would expect r to be a zero terminated string[/color]
of[color=blue]
> length 1. Am I wrong?[/color]

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'.
[color=blue]
> The STL I'm using omit the zeroterm in this situation.[/color]

Chuck it. Get a decent implementation.
[color=blue]
> 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;[/color]

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

Victor


klaus hoffmann
Guest
 
Posts: n/a
#3: Jul 19 '05

re: missing zeroterminator in string.c_str()?


klaus hoffmann schrieb:[color=blue]
>[/color]
[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
Closed Thread