I'm a trifle confused about the use of strings. In my Greenhouse Controller Sketch I want to display the sketch version on the LCD. I THINK I have the string set up correctly because the compiler likes it -
char Str1[]="V2/30 I2";
I can't see how to get this to display.
lcd.print(char Str1[]); isn't accepted.
Advice please
I can't see how to get this to display.
The function expects a string. So, pass it one. Str1[] is NOT a string. Str1 is.
Maybe this will help you understand the difference. When you defined the string:
char Str1[]="V2/30 I2";
the compiler counted the number of characters in the string (i.e., 8 ) and added 1 to it. The first 8 characters are the data, the last character is the null character ('\0') which is used to indicate the end of the string has been reached. To store your variable, the compiler looks for 9 bytes of free memory. Let's say it finds it at memory address 1000. The compiler then links memory address 1000 with your character array and stores that information temporarily in a table called the symbol table.
Now, if later you write:
Serial.println(Str1);
the compiler looks in the symbol table, sees the variable is located at memory address 1000, and start displaying characters from 1000 until it reads the null character. If you wrote instead:
Serial.println(Str1[2]);
you are now asking for element 2 of the string. Since C strings start with 0, the compiler generates code to display a single characters at memory location 1002, or the '/' character.
Conclusion: an array name by itself is the same as the starting memory address of the array. It is also the same as:
&Str1[0]
The "address of" operator (&) returns the memory address of a variable. The memory address of Str1[0] is also memory address 1000.
If all this makes sense, ask yourself what this does:
void setup()
{
char Str1[]="V2/30 I2";
char temp[10];
Serial.begin(9600);
strcpy(temp, &Str1[1]);
strcat(temp, "+");
Serial.println(temp);
}
What is displayed?