# Trouble understanding how to use Char \* variable

**URL:** <https://community.particle.io/t/trouble-understanding-how-to-use-char-variable/66948>\
**Category:** Firmware\
**Created:** [May 31, 2024, 12:30am UTC](https://community.particle.io/t/trouble-understanding-how-to-use-char-variable/66948 "2024-05-31T00:30:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimbol](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jimbol](https://community.particle.io/u/jimbol)\
**Post date:** [May 31, 2024, 12:30am UTC](https://community.particle.io/t/trouble-understanding-how-to-use-char-variable/66948/1 "2024-05-31T00:30:07Z")

</div>

Hi All,

I have code that will compile and run, but has a bunch of warnings about some variables I use.

I declare a variable before setup, and then during the setup routine, I use the particle ID to get site specific details. I'll use one here which is an API key - the API key gets the particular site data to the right graph so we understand where it came from.

I declare a variable:

> char \* myWriteAPIKey;

Then I put the data into the variable:

> if (myID.equals("e00fce68370b9dd38b709203")) {  
> myWriteAPIKey = "APIKEYGOESHERE";  
> }

but I get the following problem during compile:

> warning: ISO C++ forbids converting a string constant to 'char\*' [-Wwrite-strings]

how do I get the char\* or string formatted properly?

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [May 31, 2024, 9:16am UTC](https://community.particle.io/t/trouble-understanding-how-to-use-char-variable/66948/2 "2024-05-31T09:16:42Z")

</div>

That will happen, and there are two common fixes:

### const char \*

You can change the global variable declaration to:

```auto
const char * myWriteAPIKey;

```

The reason for this is that with a `const char *` you can change the pointer, but you can't change the contents of the things that are pointed to. So you can assign it to a different string, but you couldn't change only the first character of the string, for example.

This is important because the string in `myWriteAPIKey = "APIKEYGOESHERE"`is a const string. The string is stored in flash memory with the code and can't be modified at runtime, so you can't assign it to a `char *` variable because that means the string to be modified, and it can't be modified.

### String

The other common solution is to declare it like this:

```auto
String myWriteAPIKey;

```

The main difference is that when you do the assignment, you are making a copy of the string. So the const string `"APIKEYGOESHERE"` still exists in flash memory in the code, but when you assign it, a copy is made to `myWriteAPIKey` on the heap. That allows it to be modified later, if desired.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/4/64635adccc35238caa19e9b553ec3e98b163d026.png) [@system](https://community.particle.io/u/system)\
**Post date:** [November 29, 2024, 9:17pm UTC](https://community.particle.io/t/trouble-understanding-how-to-use-char-variable/66948/3 "2024-11-29T21:17:20Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
