# Are string variables fully supported? \[SOLVED\]

**URL:** <https://community.particle.io/t/are-string-variables-fully-supported-solved/3332>\
**Category:** General\
**Created:** [March 10, 2014, 12:07am UTC](https://community.particle.io/t/are-string-variables-fully-supported-solved/3332 "2014-03-10T00:07:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tforster](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/tforster/32/20943_2.png) [@tforster](https://community.particle.io/u/tforster)\
**Post date:** [March 10, 2014, 12:07am UTC](https://community.particle.io/t/are-string-variables-fully-supported-solved/3332/1 "2014-03-10T00:07:49Z")

</div>

According to [http://docs.spark.io/#/firmware/data-and-control-spark-variable](http://docs.spark.io/#/firmware/data-and-control-spark-variable) it is possible to return ints, doubles and strings. My particular use case requires an array of bytes so string seems to be the closest possible supported datatype (my client app will have to string split and convert bytes accordingly).

However, I am unable to retrieve a string from a Spark.variable. More accurately, I cannot change the value of an initialized string and return it.

I have globally declared  
`char *colour[128]; // Once working will be [3] to represent RGB bytes`

and in Setup()  
`Spark.variable("colour", &colour, STRING);`

I have a Spark.function `fadeTo(String command)` that attempts to set the global colour variable to the command value. Since the Spark function POST param is String and the global var is char[] I have tried the following to convert/cast:  
`char charBuf[128]; command.toCharArray(charBuf, 128); colour = command;`  
But that returns

_error: incompatible types in assignment of ‘String’ to 'char_ [128]’\*

Two questions:

1. Why does the documented example for returning strings actually use a character array and not a string?
2. What is the correct way to implement a RESTful interface that supports strings for both POSTs (Spark.functions) and GETs (Spark.variables)?

---

<div class="post-metadata">

**Author:** ![Hypnopompia](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hypnopompia/32/9039_2.png) [@Hypnopompia](https://community.particle.io/u/Hypnopompia)\
**Post date:** [March 10, 2014, 12:44am UTC](https://community.particle.io/t/are-string-variables-fully-supported-solved/3332/2 "2014-03-10T00:44:54Z")

</div>

@tforster Yep, strings are supported. Here’s some code that I was able to get working that’s similar to your example.

```
char colour[10] = "foo";

void setup() {
    Spark.variable("colour", colour, STRING);
    Spark.function("fadeTo", fadeTo);
}

void loop() {

}

int fadeTo(String command) {
    char charBuf[10];
    command.toCharArray(charBuf, 10);
    strcpy(colour, charBuf);
    return 1;
} 
```

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [March 10, 2014, 12:57am UTC](https://community.particle.io/t/are-string-variables-fully-supported-solved/3332/3 "2014-03-10T00:57:44Z")

</div>

Hi @tforster

@Hypnopompia is right–string variable are supported! I think your problem is that you declared colour as `char*` and then used `&colour` in the Spark.variable call. If you look at TJ’s example, it uses `char colour` (not `char*`) and then uses `colour` in the Spark.variable, which is the same as `&colour` for an array.

Let us know if that doesn’t work for you and we will keep digging!

---

<div class="post-metadata">

**Author:** ![tforster](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/tforster/32/20943_2.png) [@tforster](https://community.particle.io/u/tforster)\
**Post date:** [March 10, 2014, 1:06am UTC](https://community.particle.io/t/are-string-variables-fully-supported-solved/3332/4 "2014-03-10T01:06:11Z")

</div>

@Hypnopompia’s example is working for me. I think the difference was strcpy() which I had in one of my attempts but possibly with some other syntax issues. Colour was declared as char\* based on the sample in the documentation. I’ve been working on this all night and gone through numerous iterations but thanks to @Hypnopompia it is working now.

I haven’t worked with C or C++ for a couple of decades but have strength in C#. I think its time to brush up on the arcane C++ syntax, pointers and ampersands.

---

<div class="post-metadata">

**Author:** ![fbeachler](https://avatars.discourse-cdn.com/v4/letter/f/e19b73/32.png) [@fbeachler](https://community.particle.io/u/fbeachler)\
**Post date:** [January 10, 2016, 3:01am UTC](https://community.particle.io/t/are-string-variables-fully-supported-solved/3332/6 "2016-01-10T03:01:54Z")

</div>

Forgive me for being so opinionated, but I couldn’t help it – we’re talking about C++, designed by groups of academicians who really should be cross-examined for misguiding generations of software engineers. Thus my question is, which takes less time and is more valuable: to build a cross-compiler for C# that targets P0/P1, or learn C++ constructs?
