# Char \* and juce::String

**URL:** <https://forum.juce.com/t/char-and-juce-string/1138>\
**Category:** General JUCE discussion\
**Created:** [October 12, 2006, 9:04pm UTC](https://forum.juce.com/t/char-and-juce-string/1138 "2006-10-12T21:04:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbottoms](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@rbottoms](https://forum.juce.com/u/rbottoms)\
**Post date:** [October 12, 2006, 9:04pm UTC](https://forum.juce.com/t/char-and-juce-string/1138/1 "2006-10-12T21:04:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rbottoms](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@rbottoms](https://forum.juce.com/u/rbottoms)\
**Post date:** [October 12, 2006, 9:33pm UTC](https://forum.juce.com/t/char-and-juce-string/1138/2 "2006-10-12T21:33:47Z")

</div>

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](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)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [October 13, 2006, 9:24am UTC](https://forum.juce.com/t/char-and-juce-string/1138/3 "2006-10-13T09:24:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 12, 2017, 9:59am UTC](https://forum.juce.com/t/char-and-juce-string/1138/4 "2017-05-12T09:59:47Z")

</div>


