char* combination problem :(

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • x

    char* combination problem :(

    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
  • Christopher Benson-Manica

    #2
    Re: char* combination problem :(

    x <aotemp@hotmail .com> spoke thus:
    [color=blue]
    > Im trying to convert a few variables into a string object ( a
    > character string object). Only I have forgotten how! how
    > embarrasing....[/color]
    [color=blue]
    > char *x = (int1 + "/" + int2 + "/" + int3);[/color]
    [color=blue]
    > whats wrong? int1-3 can equal 0-99...[/color]

    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.




































    [color=blue]
    > 6e[/color]

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

    Comment

    • Rolf Magnus

      #3
      Re: char* combination problem :(

      x wrote:
      [color=blue]
      > 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?[/color]

      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::stringstre am stream;
      stream << int1 << "/" << int2 << "/" << int3;
      std::string x = stream.str();

      Comment

      • x

        #4
        Re: char* combination problem :(

        Rolf Magnus <ramagnus@t-online.de> wrote in message news:<c5f1h1$51 c$03$1@news.t-online.com>...[color=blue]
        > x wrote:
        >[color=green]
        > > 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?[/color]
        >
        > 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::stringstre am stream;
        > stream << int1 << "/" << int2 << "/" << int3;
        > std::string x = stream.str();[/color]


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

        Comment

        • Christopher Benson-Manica

          #5
          Re: char* combination problem :(

          x <aotemp@hotmail .com> spoke thus:
          [color=blue]
          > the only problem with that is that the function I am tryin to use the
          > string of characters with requires an input of char*......[/color]

          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)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • Bill Seurer

            #6
            Re: char* combination problem :(

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

            You can extract a char* from a string. See the functions in the string
            class.

            Comment

            • Kevin Goodsell

              #7
              Re: char* combination problem :(

              Christopher Benson-Manica wrote:[color=blue]
              > x <aotemp@hotmail .com> spoke thus:
              >
              >[color=green]
              >>the only problem with that is that the function I am tryin to use the
              >>string of characters with requires an input of char*......[/color]
              >
              >
              > 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 );
              >[/color]

              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<cha r> 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<Cha rType>
              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.

              Comment

              • Christopher Benson-Manica

                #8
                Re: char* combination problem :(

                Kevin Goodsell <usenet2.spamfr ee.fusion@never box.com> spoke thus:
                [color=blue]
                > strcpy(buf, x.c_str()); // Be careful!![/color]

                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)cybers pace.org | don't, I need to know. Flames welcome.

                Comment

                • Sam Dennis

                  #9
                  Re: char* combination problem :(

                  Christopher Benson-Manica wrote:[color=blue]
                  > Kevin Goodsell <usenet2.spamfr ee.fusion@never box.com> spoke thus:[color=green]
                  >> strcpy(buf, x.c_str()); // Be careful!![/color]
                  >
                  > Or better,
                  >
                  > strncpy( buf, sizeof buf - 1, x.c_str() );[/color]

                  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"

                  Comment

                  • Christopher Benson-Manica

                    #10
                    Re: char* combination problem :(

                    Sam Dennis <sam@malfunctio n.screaming.net > spoke thus:
                    [color=blue][color=green]
                    >> strncpy( buf, sizeof buf - 1, x.c_str() );[/color][/color]
                    [color=blue]
                    > 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.)[/color]

                    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.
                    [color=blue]
                    > If you must do this sort of thing in C++, this should be safe:[/color]
                    [color=blue]
                    > char buf[N];
                    > *buf = 0;
                    > strncat( buf, x.c_str(), sizeof buf - 1 );[/color]

                    Agreed.

                    --
                    Christopher Benson-Manica | I *should* know what I'm talking about - if I
                    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                    Comment

                    Working...