# Const char\* and char\*

**URL:** <https://forum.juce.com/t/const-char-and-char/7661>\
**Category:** General JUCE discussion\
**Created:** [November 4, 2011, 9:48pm UTC](https://forum.juce.com/t/const-char-and-char/7661 "2011-11-04T21:48:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![eamonzo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eamonzo/32/362_2.png) [@eamonzo](https://forum.juce.com/u/eamonzo)\
**Post date:** [November 4, 2011, 9:48pm UTC](https://forum.juce.com/t/const-char-and-char/7661/1 "2011-11-04T21:48:37Z")

</div>

I am using the Csound API which requires a char\* filepath to compile a csound file.  
I convert the string to a const char\* using:

`const char* caFilePath = sFilePath.toUTF8();
csound->Compile(caFilePath )
`

The problem is Csound won’t accept a const char\* as a parameter, only a char\*, is there a way to convert?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [November 4, 2011, 10:16pm UTC](https://forum.juce.com/t/const-char-and-char/7661/2 "2011-11-04T22:16:08Z")

</div>

const\_cast \<char\*\> (caFilePath)?

---

<div class="post-metadata">

**Author:** ![eamonzo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eamonzo/32/362_2.png) [@eamonzo](https://forum.juce.com/u/eamonzo)\
**Post date:** [November 4, 2011, 10:35pm UTC](https://forum.juce.com/t/const-char-and-char/7661/3 "2011-11-04T22:35:52Z")

</div>

Awesome, thanks.

---

<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:** [November 5, 2011, 11:27am UTC](https://forum.juce.com/t/const-char-and-char/7661/4 "2011-11-05T11:27:13Z")

</div>

Are you sure the CSound function isn’t going to modify the contents of your string?

If it really is a read-only parameter, then it’s an absolutely shameful bit of programming for them not to make the parameter a const char\*! You should write and tell them!

---

<div class="post-metadata">

**Author:** ![eamonzo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eamonzo/32/362_2.png) [@eamonzo](https://forum.juce.com/u/eamonzo)\
**Post date:** [November 5, 2011, 7:20pm UTC](https://forum.juce.com/t/const-char-and-char/7661/5 "2011-11-05T19:20:25Z")

</div>

I feel like the problems I’m having are very basic (apologies).

Csound takes a normal char\* but the issue I have is getting a Juce String into a char\*.  
Is there a better way to do it than what I have below?

`const char* chPath = sFPath.toUTF8();
chFilePath = const_cast <char*> (chPath);`

chFilePath is a private char\* member variable and I’m also getting a runtime access violation error on the second line of code.

Thanks

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [November 6, 2011, 3:57pm UTC](https://forum.juce.com/t/const-char-and-char/7661/6 "2011-11-06T15:57:32Z")

</div>

You can use the CharPointer\_UTF8::getAddress() method:

csound-\>Compile( const\_cast\<char\*\>(sFPath.toUTF8().getAddress()));

where sFPath is a Juce::String.

---

<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:** [November 6, 2011, 5:12pm UTC](https://forum.juce.com/t/const-char-and-char/7661/7 "2011-11-06T17:12:04Z")

</div>

> [@](#):
>
> chFilePath is a private char\* member variable

Have a careful read of the comments for String::toUTF8() - it gives you a _temporary_ pointer, don’t keep it in a member variable!

---

<div class="post-metadata">

**Author:** ![eamonzo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eamonzo/32/362_2.png) [@eamonzo](https://forum.juce.com/u/eamonzo)\
**Post date:** [November 6, 2011, 8:02pm UTC](https://forum.juce.com/t/const-char-and-char/7661/8 "2011-11-06T20:02:50Z")

</div>

Thanks Rory!

Unfortunately I still get an access violation error on this line:

```auto

```

that breaks to here:

`	/** Lets you access methods and properties of the object that this ScopedPointer refers to. */
	inline ObjectType* operator->() const noexcept { return object; }`

---

<div class="post-metadata">

**Author:** ![rory](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rory/32/534_2.png) [@rory](https://forum.juce.com/u/rory)\
**Post date:** [November 7, 2011, 12:51am UTC](https://forum.juce.com/t/const-char-and-char/7661/9 "2011-11-07T00:51:21Z")

</div>

The following works fine here:

String inputfile;  
inputfile = “C:/MyDocuments/SourceCode/test/Builds/VisualStudio2010/Debug/test.csd”;  
csCompileResult = csound-\>Compile(const\_cast\<char\*\>(inputfile.toUTF8()));

---

<div class="post-metadata">

**Author:** ![eamonzo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eamonzo/32/362_2.png) [@eamonzo](https://forum.juce.com/u/eamonzo)\
**Post date:** [November 7, 2011, 9:07pm UTC](https://forum.juce.com/t/const-char-and-char/7661/10 "2011-11-07T21:07:13Z")

</div>

Hmm, that gives me the following error:

“error C2440: ‘const\_cast’ : cannot convert from ‘const juce::CharPointer\_UTF8’ to 'char \*'  
1\> Conversion requires a constructor or user-defined-conversion operator, which can’t be used by const\_cast or reinterpret\_cast”

with the inputFile String underlined.

---

<div class="post-metadata">

**Author:** ![eamonzo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eamonzo/32/362_2.png) [@eamonzo](https://forum.juce.com/u/eamonzo)\
**Post date:** [November 7, 2011, 11:15pm UTC](https://forum.juce.com/t/const-char-and-char/7661/11 "2011-11-07T23:15:31Z")

</div>

> [@jules](#):
>
> [quote]chFilePath is a private char\* member variable

Have a careful read of the comments for String::toUTF8() - it gives you a _temporary_ pointer, don’t keep it in a member variable![/quote]

I missed this earlier, thanks!
