Convert string data type to char type ?

Discussion in 'C++' started by tvn007@hotmail.com, Oct 20, 2005.

  1. Guest

    Hi,

    I tried to convert string data type to char type so
    that I can use strtok.

    I have used c_str(). However, still getting error message
    when compile .

    Any help would be appreciate.

    Thanks,
    , Oct 20, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...

    > I tried to convert string data type to char type so
    > that I can use strtok.
    >
    > I have used c_str(). However, still getting error message
    > when compile .
    >
    > Any help would be appreciate.


    It is very difficult to help without seeing your code. In this case, my
    guess is that you are passing what c_str() returns to strtok.

    If so, look more closely and realize that what c_str() returns is not
    compatible with what strtok takes: 'char const *' vs. 'char *'.

    Ali
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Oct 20, 2005
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > Hi,
    >
    > I tried to convert string data type to char type so
    > that I can use strtok.
    >
    > I have used c_str(). However, still getting error message


    Which errors?

    > when compile .


    When you compile what?

    My crystal ball came just back from repair.
    It says that your errors might have to do with the fact that strtok()
    modifies the data which is not allowed on what c_str() returns.
    Rolf Magnus, Oct 20, 2005
    #3
  4. Guest

    ptr =strtok(answer.c_str()," \t\n,()");
    Error message:
    invalid conversion from `const char*' to `char*'
    , Oct 20, 2005
    #4
  5. <> wrote in message
    news:...
    > ptr =strtok(answer.c_str()," \t\n,()");
    > Error message:
    > invalid conversion from `const char*' to `char*'


    Please quote what you are replying to.

    In this case, my guess is that you are showing us some code. Thank you... As
    I said in my previous post, look more closely and realize that what c_str()
    returns is not compatible with what strtok takes.

    In other words, please check the documentations for c_str and strtok. You
    may realize that strtok takes a 'char*' and unfortunately, c_str DOES NOT
    return that type.

    You need to copy the characters that are returned by c_str into a buffer,
    then call strtok with that buffer:

    #include <string.h>
    /* ... */

    // WARNING: buffer may be leaked if an exception is thrown
    // before getting to the free(buffer) call below
    //
    // Also, strdup is not standard but exists on many systems

    char * buffer = strdup(answer.c_str());
    ptr = strtok(buffer, /* ... */);
    /* ... */
    free(buffer);

    Ali
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Oct 20, 2005
    #5
  6. Guest

    Ali, Thanks a million for your quick help.
    , Oct 20, 2005
    #6
  7. Greg Comeau Guest

    In article <>,
    <> wrote:
    >I tried to convert string data type to char type so
    >that I can use strtok.
    >
    >I have used c_str(). However, still getting error message
    >when compile .
    >
    >Any help would be appreciate.


    We don't know exactly what you did since you don't show
    a working snippet. In the meantime, this may help:
    http://www.comeaucomputing.com/techtalk/#atoi
    It may not be what you want, but may have examples enough
    to get you through your problem.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Oct 20, 2005
    #7
    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. luna
    Replies:
    1
    Views:
    6,817
  2. wwj
    Replies:
    7
    Views:
    549
  3. wwj
    Replies:
    24
    Views:
    2,507
    Mike Wahler
    Nov 7, 2003
  4. Ben Pfaff
    Replies:
    5
    Views:
    473
    Tristan Miller
    Jan 17, 2004
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
Loading...

Share This Page