std::string to char *

  • Thread starter Thread starter Abubakar
  • Start date Start date
Hi,
how do I convert std::string to char * ?

you can use PtrToStringChars
because all managed strings are in unicode, this will give you a unicode
string. you can then go from unicode to ansi if you like.

see MSDN for more details. it has an example that describes how to do this.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Hi,
how do I convert std::string to char * ?

Oops.
previous reply was for managed strings.
for std::string you can just use the c_str() method.

std::string myString("Hello");

const char * ptr = myString.c_str();

remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not
modified in any way.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
const char * ptr = myString.c_str();

And I wont have to worry about "delete"ing the ptr right?

-Ab.
 
const char * ptr = myString.c_str();
And I wont have to worry about "delete"ing the ptr right?

no. definitly not. the buffer that is returned is managed by the string
object itself.

That is also the reason why you shouldn't use it after the string changes.
The standard says that that pointer is only valid while the string stays the
same.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top