I was wondering if the following would auto-pad with NULL characters up to the full 64 bytes?
char testArray[64] = "NotAFull64Bytes";
Thanks in advance for any help.
I was wondering if the following would auto-pad with NULL characters up to the full 64 bytes?
char testArray[64] = "NotAFull64Bytes";
Thanks in advance for any help.
Easy to test?
Just Serial.println(testArray[20]), I'd say
I'd be interested in the result of that aswell.
No need to test. The array is auto-padded.
I would qualify that by saying that the array is zero-padded when it is global or static. I don't know that it is padded when it is automatic.
-Mike
Good point. I don't know either.
@Mike Murdock: I'm not sure I fully understand what you mean by that. Can you please elaborate?
Thanks heaps.
An automatic variable is a variable placed on the stack (as opposed to a variable placed in the data area). It is automatically created and destroyed as needed.
For example...
char StaticArray[10]; // <-- this is static because it is not inside a function
void setup( void )
{
char AutomaticArray[12]; // <-- this is automatic because it is inside a function
}
void loop( void )
{
static char AnotherStatic[4]; // <-- this one is static because we declared it static
}
Does that help?
@Coding Badly: Excellent. Thanks for that explanation.
So, going by that, the below example would remain with the null padding?
char testArray[64];
void setup() {
testArray[64] = "NotAFull64Bytes";
}
What if we then did this:
char testArray[64];
void setup() {
testArray[64] = "NotAFull64Bytes";
testArray[64] = "EvenLess";
}
Would the value then be "EvenLess\0\0\0....", or would it be "EvenLess64Bytes\0\0\0..."?
Thanks for that explanation.
You are welcome.
This is filled with nulls before setup is called...
char testArray[64];
This assignment will not work the way you expect...
void setup() {
testArray[64] = "NotAFull64Bytes";
}
testArray[64] references a single character past the end of your array. To save "NotAFull64Bytes" into your array of characters you have to do something like this...
strcpy( testArray, "NotAFull64Bytes" );
After doing the strcpy, the extra elements of testArray passed the 's' will still have nulls.
In this example...
void setup() {
strcpy( testArray, "NotAFull64Bytes" );
strcpy( testArray, "EvenLess" );
}
After setup is called, testArray contains the following (where ~ marks the nulls)...
EvenLess~4Bytes~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It's important to remember that there is no native string type in Arduino. "Strings" are really an array of characters.