how to convert string to char* and vice versa

does anybody know? actually, because i want to use strprbk() by users input...
std::string has a member c_str() which returns a C-style array.
To "convert" a C array char * to a C++ std::string simply use the latter's constructor, or assignment operator. Example:

1
2
3
4
5
6
7
char something[100];
std::string something_else = "A thing.";

std::strcpy(something, something_else.c_str());

std::string anewstring(something); // same as
anewstring = something;


http://www.cplusplus.com/reference/string/string/
Last edited on
btw, why string is defined as const char * and not char*?
string is not const char * !!!!
your mean is why c_str() return const char * ?

you can change this but not good idea
1
2
string str;
strcpy(const_cast<char *>( str.c_str() ), "hello");


and we dont have function "strprbk" i think your mean is "strpbrk" !!!
Last edited on
hmm... really? (guess i've read it somewhere that string is const char*, but nevermind, don't really remember :)) ) then, why string is not defined as char*?
umm i cant understand your mean !!! but i have one question : if is const char , how can change it ?
for exm :
if string is const char then this is not be true !!!

string str;
str = "hello"; // why can i change it ?!!

string is typedef of basic_string<char, char_traits<char>, allocator<char> >
Last edited on
ok man i found for you some reference if you dont have trust to me !!!
in C++ CookBook "herbert schildt" is wrote :
"The first type of string supported by C++ is the null-terminated string. This is a char array
that contains the characters that comprise a string, followed by a null. The null-terminated
string is inherited from C and it gives you low-level control over string operations."
as you see , char array no const char array
Topic archived. No new replies allowed.