Char * and juce::String


#1

I know it’s my lameness with C++ but:

I have a char * _userName

I have a juce:String uidBox->getText();

How do I convert the juce::String to char * so I can do this:

char _cmd[255];
sprintf(_cmd, “+set name %s”, _userName );

This is the last obstacle to getting my demo out the door.


#2

After hours of trying to find the right Google query to answer this question I found it in Google’s cache.

HOWTO: Convert juce::String to char *

const char * _userName = (char *)(const char *) uidBox->getText();

http://72.14.253.104/search?q=cache:TjCD7mIspMYJ:www.adbe.org/juceforum/viewtopic.php%3Ft%3D138%26start%3D0%26postdays%3D0%26postorder%3Dasc%26highlight%3D%26sid%3D461ae5511e7c5f999368c82077f973b9+juce::string+char+*&hl=en&gl=us&ct=clnk&cd=1


#3

What’s wrong with
const char * _userName = (const char *) uidBox->getText();
?

But you should be really careful converting strings to char*, and never do it unless there’s absolutely no alternative. The array that’s returned is transient, and will become invalid if the string goes out of scope or changes in any way. This is all explained in the comments for the String class casting operators.


#4