Hi,
i want to convert a string to char * or char[]
i tried:
c_str() returns a const char* . I dont want that. I just want char* or char [] only.Code:string a="123"; char *b=a.c_str();
any way to solve the problem?
thanks,
Rahul
Hi,
i want to convert a string to char * or char[]
i tried:
c_str() returns a const char* . I dont want that. I just want char* or char [] only.Code:string a="123"; char *b=a.c_str();
any way to solve the problem?
thanks,
Rahul
Well... why do you want to do this?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
The error is as stated above. cant convert from char* to const char*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; }
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; } }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)