I am using the StackString function to create character arrays for my sketch string descriptors.
But I am doing something incorrect regarding the establishment of the array and later referencing it.
This is causing a compiler warning:
error: incompatible types in assignment of 'const char [3]' to 'char [4]'
I am setting up an initial assignment of the array and then referencing it later in the sketch.
char Fan_Status[4] = "OFF"; // initial declaration; Fan on/off status
if (digitalRead(Fan_outPin) == HIGH)
{
char Fan_Status[4] = "ON"; // establish fan 'ON' status
}
If I remove the [4] in the 'if' statement the error changes to:
warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
So I am violating some sort of C syntax rule that I am unaware of right now.
char Fan_Status[4] = "OFF";// this is a global variable with global scope
if (digitalRead(Fan_outPin) == HIGH) {
char Fan_Status[4] = "ON";// local variable with local scope
}
You do not need to use StackString or any other wrapper around the basic string. "OFF" is a string literal or otherwise known as a const char [4] and likely if you put a space after on such as "ON " you might get another error or perhaps silence all errors, but your program would not work as you want. Study up on c-style strings (aka nul-terminated char arrays).
According to other forum member using StackString can help prevent microcontroller memory fragments and potentially erratic behavior from standard string usage. I hope I got that right. lol
I am using StackString for translating descriptors to browser webpages. Things like temps, setpoints, etc where the nomenclature is much longer than just 'ON' or 'OFF.' All in an attempt to minimize string-induced memory fragmentation and possibly subsequent erratic behavior.
This definitely works, is simple, and efficient. And from what I understand as a C neophyte reading a Beginner's C manual the "strcpy()" function simply modifies the existing character array; not impacting memory negatively.
When you put char Fan_Status[4] = "ON";
You are declaring a new variable, and placing an initial value in it. To use one you already have, you have to refer to it differently. Beside the strcpy() others suggested, you could use
This is cumbersome, and not a lot faster than calling strcpy(). The difference is that I didn't place a type (char) on any of the lines. The compiler already knows from the declaration that's what it is.
Note that I placed a character with a value of zero into the third element of the array. Strings in C programs are expected to terminate with a NULL. If I had only coded the first two lines, it would print as "ONF" because the null was after the 3rd character originally, and all we did was overwrite the first two with O and N.
Thanks for that info. What you covered is similarly laid out in my C Absolute Beginners Guide. Maybe you are a good candidate for a C guidebook of your own. Lol
I have enjoyed learning C (started a couple months ago) but have discovered the depth and breadth of the language is fairly vast depending on what level you are reaching for. I am learning just enough to write sketches for controlling and monitoring equipment in my support buildings on my property. But I do enjoy coding its just a matter of how much time does a person want to invest in it.
The other thing I have learned is there is numerous coding approaches to accomplish the same task. Some obviously more efficient and less memory hungry than others.
As long as the new string is shorter than the original, there will not be a problem. If it's longer, it will cause memory corruption.
E.g. this will definitely cause memory corruption. The symptoms can vary from none to another variable being corrupted to a crash or restart (and anything in between that).
My point isn't that StackString is not better than the default Arduino String Class, but that it creates a dependency that may not be portable and does not add anything to the c-string. string.h is already built into Arduino and remains portable to most AVR microcontrollers. Additionally, it is a subset of the c standard so it is very likely portable to any microcontroller and or IDE that implements that standard. If you look at the source code for StackString, you will find that it simply calls c-string functions to perform its magic. This is what we call a wrapper.
I was wondering about that when you mentioned it earlier. In having this offline issue I
have come to learn a great deal more about C and especially strings and arrays. Which is a good thing to my thinking.