std::string to char *

G

Guest

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"
 
G

Guest

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"
 
A

Abubakar

const char * ptr = myString.c_str();

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

-Ab.
 
G

Guest

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top