Continuing on from @peekay123 ...
Because a, "string" in C, like in the case of message
in the example you gave, is actually already a pointer to an array of chars. In this case, the & is implicit -- it is logically assumed by the compiler that, since message
is a label associated with an array, it can only hold a pointer -- the address-of -- that array.
Yes, it's a little confusing until it becomes habit, especially if you're coming at it from higher level languages, like Python, BASIC or PHP, to name but a few.
In other words, the following two declarations are identical, the second being simply a kind of convenient short-hand, which we tend to use for string constants, because it's much easier to type and read ...
char string[] = {'A', ' ', 's', 't', 'r', 'i', 'n', 'g', '\0'};
char *string = "A string";
In each case, the reference string
is a pointer to an array of chars, as opposed to a variable holding an actual value, as in the case of an int
or even a single char, as in char singleChar = 'A';
The asterix in front of *string
means that we are declaring a pointer. What we are pointing to, is an array of chars, created by the shorthand, "A string". Thus, string
on its own, without the asterix, is the pointer itself, which holds only the address in memory where the array of chars can be found. For Spark.variable, this is what we want -- to tell the function where to find our string. (Because we may change the contents of the string's array of chars, you see.)
And last but not least, if we were to type &string
, we would be saying, "here is the address a the pointer (that points to our array or chars)", which is not what we want.
Hope that makes sense -- and more so that I got it 100% correct!
P.S: You can even write the following, to mean the same as the two examples above. All three version result in identical compiled code, ...
char string[] = "A string";
When you use the "string" syntax, the compiler automatically adds a null terminating character on the end, which we had to do ourselves, when we declared the array manually, above. By convention, all C strings end with a null -- character code zero.
Of trivial interest, perhaps -- In other languages, like Pascal for example, the length of the string is stored in the first array element instead and there is no terminating null. Historically therefore, Pascal strings were limited to 255 characters in length. C strings essentially have no such limit.