incompatible types in assignment of 'const char [5]' to 'char [30]'
From this code
void setup() {
// put your setup code here, to run once:
struct myID
{
unsigned int MyNumber;
char MyName[30];
} Me;
Me.MyNumber = 3;
Me.MyName ="Fred";
}
void loop() {
// put your main code here, to run repeatedly:
}
As I understand it, I can declare a string of characters longer than I need and the assign any shorter string of characters to it.
"=" copies a single object and an array of chars is more that one object
"Fred" refers to the location of that string (of characters) in memory. so when you write = "Fred", you're saying copy the location of "Fred" in memory to the left hand side of the "=".
you would typically use strcpy to copy the array of characters to another array of characters
you would also assign the location of a string to a pointer -- char *s = "Fred"
i would suggest to statically initialize a variable of the structure.
struct myID
{
unsigned int MyNumber;
char MyName[30];
};
struct myID Me = {3, "Fred" };
All of the following are valid declarations for strings.
char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
Possibilities for declaring strings
Declare an array of chars without initializing it as in Str1
Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str2
Explicitly add the null character, Str3
Initialize with a string constant in quotation marks; the compiler
will size the array to fit the string constant and a terminating null character, Str4
Initialize the array with an explicit size and string constant, Str5
Initialize the array, leaving extra space for a larger string, Str6
Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str2
Be careful with this. The compiler is only guaranteed to add the required zero if the string is declared globally or locally as static. As written, if it is simply declared local then there is no way of knowing what the final character will be.
This works (for globals) only because entries that are not explicitly initialized are initialized to zero and you explicitly set the array size to one greater than the number of initializers.
This does not work because the array length is set to the number of explicitly initialized entries. You end up without a null terminator.
[copde]char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str3a[] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};[/code]
This works in both cases because you explicitly include a null terminator.