string to char* or char[]

This is a discussion on string to char* or char[] within the C++ Programming forums, part of the General Programming Boards category; Hi, i want to convert a string to char * or char[] i tried: Code: string a="123"; char *b=a.c_str(); c_str() ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    string to char* or char[]

    Hi,
    i want to convert a string to char * or char[]

    i tried:
    Code:
    string a="123";
    char *b=a.c_str();
    c_str() returns a const char* . I dont want that. I just want char* or char [] only.

    any way to solve the problem?

    thanks,
    Rahul

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,803
    Well... why do you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    I want to tokenise a string(in string format) base on specified delimiters.

    Code:
    void getTokens(char str[],char delims[]){
    	
         char *result = NULL;
         result = strtok( str, delims );
         while( result != NULL ) {
             cout<<result<<endl;
             result = strtok( NULL, delims );
         }
    }

    Then main function actually intends to pass in a string.

    Code:
    int main(void){
    	
    	string a="a#bc#def#ghij#klmno";
    	//SINCE I CAN'T PASS IN A STRING,HAVE TO PASS IN A CHAR*
    
            char* rt=a.c_str(); 
        //error here.cant convert from const char* to char*
    	getTokens(rt,"#");
    	return 1;
    }
    The error is as stated above. cant convert from char* to const char*

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,803
    Instead of using strtok(), use the functionality provided by std::string, e.g.,
    Code:
    void getTokens(const std::string& str, const std::string& delimiters) {
        using std::string;
        string::size_type index = 0;
        while ((index = str.find_first_not_of(delimiters, index)) != string::npos) {
            string::size_type end_index = str.find_first_of(delimiters, index);
            std::cout << str.substr(index, end_index - index) << std::endl;
            index = end_index;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21