char* combination problem :(

Discussion in 'C++' started by x, Apr 12, 2004.

  1. x

    x Guest

    Im trying to convert a few variables into a string object ( a
    character string object). Only I have forgotten how! how
    embarrasing....

    char *x = (int1 + "/" + int2 + "/" + int3);

    whats wrong? int1-3 can equal 0-99...




































    6e
    x, Apr 12, 2004
    #1
    1. Advertising

  2. x <> spoke thus:

    > Im trying to convert a few variables into a string object ( a
    > character string object). Only I have forgotten how! how
    > embarrasing....


    > char *x = (int1 + "/" + int2 + "/" + int3);


    > whats wrong? int1-3 can equal 0-99...


    Simple - you forgot what language you were using. + isn't a catch-all
    string creation operator in C++ like it is in various other languages.
    If you want a string, declare a string. A decent C++ book and the FAQ
    (posted below) will be most helpful.

    http://www.slack.net/~shiva/welcome.txt
    http://www.parashift.com/c -faq-lite/


































    > 6e


    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Apr 12, 2004
    #2
    1. Advertising

  3. x

    Rolf Magnus Guest

    x wrote:

    > Im trying to convert a few variables into a string object ( a
    > character string object). Only I have forgotten how! how
    > embarrasing....
    >
    > char *x = (int1 + "/" + int2 + "/" + int3);
    >
    > whats wrong?


    Doesn't make much sense to add integers and pointers together and write
    the result into a pointer. Rembember: char* is not a string, but a
    pointer to char. Try:

    std::stringstream stream;
    stream << int1 << "/" << int2 << "/" << int3;
    std::string x = stream.str();
    Rolf Magnus, Apr 12, 2004
    #3
  4. x

    x Guest

    Rolf Magnus <> wrote in message news:<c5f1h1$51c$03$-online.com>...
    > x wrote:
    >
    > > Im trying to convert a few variables into a string object ( a
    > > character string object). Only I have forgotten how! how
    > > embarrasing....
    > >
    > > char *x = (int1 + "/" + int2 + "/" + int3);
    > >
    > > whats wrong?

    >
    > Doesn't make much sense to add integers and pointers together and write
    > the result into a pointer. Rembember: char* is not a string, but a
    > pointer to char. Try:
    >
    > std::stringstream stream;
    > stream << int1 << "/" << int2 << "/" << int3;
    > std::string x = stream.str();



    the only problem with that is that the function I am tryin to use the
    string of characters with requires an input of char*......
    x, Apr 13, 2004
    #4
  5. x <> spoke thus:

    > the only problem with that is that the function I am tryin to use the
    > string of characters with requires an input of char*......


    Still easy, if const char* is acceptable - std::string's c_str()
    method converts the string to an array of characters, C-style. If you
    need a non-const char *, then you'll have to declare an array of
    characters and populate it appropriately (I suggest sprintf):

    char buf[256]; // larger than required, just make sure it's big enough
    // as sprintf will happily overflow the buffer if it
    // isn't

    sprintf( buf, "%d/%d/%d", int1, int2, int3 );

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Apr 13, 2004
    #5
  6. x

    Bill Seurer Guest

    x wrote:
    > the only problem with that is that the function I am tryin to use the
    > string of characters with requires an input of char*......


    You can extract a char* from a string. See the functions in the string
    class.
    Bill Seurer, Apr 13, 2004
    #6
  7. Christopher Benson-Manica wrote:
    > x <> spoke thus:
    >
    >
    >>the only problem with that is that the function I am tryin to use the
    >>string of characters with requires an input of char*......

    >
    >
    > Still easy, if const char* is acceptable - std::string's c_str()
    > method converts the string to an array of characters, C-style. If you
    > need a non-const char *, then you'll have to declare an array of
    > characters and populate it appropriately (I suggest sprintf):
    >
    > char buf[256]; // larger than required, just make sure it's big enough
    > // as sprintf will happily overflow the buffer if it
    > // isn't
    >
    > sprintf( buf, "%d/%d/%d", int1, int2, int3 );
    >


    Or you could use Rolf's answer, and copy from the std::string into your
    char array:

    strcpy(buf, x.c_str()); // Be careful!!

    Be sure to check the length first, and don't overflow buf. You could
    even do this:

    std::vector<char> vec_x(x.begin(), x.end());
    vec_x.push_back('\0');

    // function that takes (non-const) char*:
    SomeFunc(&vec_x[0]);


    It occurs to me that a streambuf that writes to a std::vector<CharType>
    could be useful in these cases -- you could skip creating a std::string
    then copying it into a vector, and write into the vector in the first place.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Apr 13, 2004
    #7
  8. Kevin Goodsell <> spoke thus:

    > strcpy(buf, x.c_str()); // Be careful!!


    Or better,

    strncpy( buf, sizeof buf - 1, x.c_str() );

    although the vector solution is probably preferable. Nice one, btw.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Apr 13, 2004
    #8
  9. x

    Sam Dennis Guest

    Christopher Benson-Manica wrote:
    > Kevin Goodsell <> spoke thus:
    >> strcpy(buf, x.c_str()); // Be careful!!

    >
    > Or better,
    >
    > strncpy( buf, sizeof buf - 1, x.c_str() );


    That's not such a great idea if one is to do C-style string processing
    on the result; strncpy doesn't guarantee null-termination. (Also, the
    arguments are in the wrong order.)

    If you must do this sort of thing in C++, this should be safe:

    char buf[N];
    *buf = 0;
    strncat( buf, x.c_str(), sizeof buf - 1 );

    --
    ++acr@,ka"
    Sam Dennis, Apr 14, 2004
    #9
  10. Sam Dennis <> spoke thus:

    >> strncpy( buf, sizeof buf - 1, x.c_str() );


    > That's not such a great idea if one is to do C-style string processing
    > on the result; strncpy doesn't guarantee null-termination. (Also, the
    > arguments are in the wrong order.)


    Yikes, I can't believe I blew it on the argument order. Well,
    actually I can... *sigh*

    As far as null-termination goes, you're right in general - but when
    strncpy is used intelligently (as I attempted to do, but failed...),
    it does null-terminate the string.

    > If you must do this sort of thing in C++, this should be safe:


    > char buf[N];
    > *buf = 0;
    > strncat( buf, x.c_str(), sizeof buf - 1 );


    Agreed.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Apr 14, 2004
    #10
    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. wwj
    Replies:
    7
    Views:
    539
  2. wwj
    Replies:
    24
    Views:
    2,480
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    458
    Tristan Miller
    Jan 17, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,011
    Ian Collins
    May 9, 2006
  5. Replies:
    3
    Views:
    720
Loading...

Share This Page