Thread: const char* to char* ?

  1. #1
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332

    const char* to char* ?

    i have a function that accepts a string as 1st parameter and char* as second parameter. the problem is these parameters are going to be used in strtok and str tk wants it'ts first parameter to be char*. ive tried converting the string useing c_str() but that generates a const char*, as i said before. anyone know how i can get around this?

    heres the code

    Code:
    void strExtract(string PARAM_entry, char* PARAM_delim)
    {
           cout<<PARAM_entry<<endl<<PARAM_delim<<endl;
           char* TMP_PARAM_entry = PARAM_entry.c_str();
           cout<<strtok(TMP_PARAM_entry, PARAM_delim);
    }
    compiler error: ( from line "char* TMP_PARAM_entry = PARAM_entry.c_str();")

    "initialization to `char *' from `const char *' discards qualifiers"


    i don't know what strtok will output but thats not really th point at this point in time

    thanks for any help

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    char* TMP_PARAM_entry = const_cast<char *>(PARAM_entry.c_str());
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User whackaxe's Avatar
    Join Date
    Mar 2004
    Posts
    332
    props to you sir! could you tell me or link me to what you just did there, the const_cast<> thingy

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by whackaxe
    props to you sir! could you tell me or link me to what you just did there, the const_cast<> thingy
    const_cast is one of four standard C++ type cast operators. (The other three are dynamic_cast, static_cast, reinterpret_cast.) The const_cast operator is for making a type cast with the sole purpose of changing whether a value is const or volatile or not.
    Here's a link that covers all of them
    google.com
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And this possibly modifies the string's internal buffer, possibly leading to a crash later. DON'T DO IT!!!!!
    In fact, don't use strtok at all. Use string_tokenizer from the boost libraries instead.
    http://www.boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM