missing zeroterminator in string.c_str()?

Discussion in 'C++' started by klaus hoffmann, Aug 13, 2003.

  1. 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;
    klaus hoffmann, Aug 13, 2003
    #1
    1. Advertising

  2. "klaus hoffmann" <> wrote...
    > 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
    Victor Bazarov, Aug 13, 2003
    #2
    1. Advertising

  3. klaus hoffmann schrieb:
    >

    [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
    klaus hoffmann, Aug 14, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Murphy
    Replies:
    2
    Views:
    626
    Tony Murphy
    Nov 11, 2003
  2. Christopher Benson-Manica

    std::string c_str()

    Christopher Benson-Manica, Feb 17, 2004, in forum: C++
    Replies:
    1
    Views:
    2,954
    Gianni Mariani
    Feb 17, 2004
  3. Derek
    Replies:
    17
    Views:
    1,155
    Leor Zolman
    May 30, 2004
  4. Vyacheslav Kononenko

    What storage does std::string::c_str() use?

    Vyacheslav Kononenko, Sep 8, 2004, in forum: C++
    Replies:
    2
    Views:
    3,605
    Old Wolf
    Sep 9, 2004
  5. Metro12
    Replies:
    18
    Views:
    945
Loading...

Share This Page