Ah, Iām not 100% sure if you intended it, but the declaration you wrote for Alpha
makes an array of strings[a]. If you want a string that stores a singular name as the user writes it out, (like a high-score list name or just any old text input) you probably just want to allocate an array with a specific size, and youād do that with the syntax char *Alpha[8]
for a string with space for 7 characters and an end-of-string marker.
[a]: cdecl
is a good tool for decoding type signatures; and strings are just pointers to char
arrays in C
Why āwith a specific sizeā?
Most programming languagesā string libraries are built upon using dynamic allocation (i.e. finding space for variables while the program is running) to get a chunk of memory to store a stringās data. As more characters are added to the string, some library function will automatically āgrowā the string (i.e. find a new larger chunk of memory and copy-paste the old chunkās contents to that new place).
The Arduboy is so absurdly small that doing these dynamic allocations can eat up a lot of your available program space, from the sheer act of the compiler bringing in an allocator and asking the program to find space on the Arduboy to store a heap. Plus, it might be outright slower to keep finding new chunks of memory. Because of this, most developers (or at the very least, me) forgo it in favor of defining every structure before the program is run.
What size should I pick?
I dunno!! 64 characters or something? Maybe even make it a compile-time constant with #define
, so you can easily change it later:
#define TEXT_INPUT_LEN 64
char Alpha[TEXT_INPUT_LEN] = { '\0' };
Just make sure to check that the userās input never exceeds that number of characters, or youāll bump up against (and possibly corrupt) other variables in your program. (Strings typically take up 1 more character than expected, as they need to store an end-of-string marker ā typically a character that is equal to ASCII 0
ā you can write it in C++ with '\0'
.)
Adding Characters to the String
Pseudocode: Find the '\0'
end-of-string marker in the array, and write characters into the array starting at that point. Once done, add the '\0'
marker to the end again.
This is written in a C coding style, and is probably not the best fit for Arduboyās C++, but itāll work.
// this function is similar to C's `strnlen` function
// and you should probably use that instead!!
uint8_t find_end_of_string(char *string, uint8_t max_length) {
for (uint8_t i = 0; i < length; i++)
if (string[i] == '\0')
return i;
return max_length - 1;
}
void add_chars_to_string(char *string, char *more_chars) {
// first, find where the string's end-of-string marker is
uint8_t end_marker = find_end_of_string(string, TEXT_INPUT_LEN);
// with that marker we just found as the starting index,
// add characters from the more_chars string to the first string.
uint8_t current_char = 0;
while ( // we haven't yet reached the end of more_chars,
more_chars[current_char] != '\0'
) {
// cancel the copy if we just ran out of space
// to put the end-of-string marker byte back in.
if (end_marker + current_char + 1 >= TEXT_INPUT_LEN) return;
// copy over a character from the second string into the first!
string[end_marker + current_char] = more_chars[current_char];
current_char++;
}
// conveniently, the previous loop we did left behind the end_marker's
// next valid position, so we can set the marker with two simple lines:
end_marker = end_marker + current_char;
string[end_marker] = '\0';
}
Removing Single Characters from the String (ābackspaceā)
Pseudocode: move the end-of-string marker back one byte, unless it would go outside the stringās bounds.
void backspace_string(char *string) {
uint8_t end_marker = find_end_of_string(string, TEXT_INPUT_LEN);
if (end_marker == 0) return; // oops! no more to backspace!
string[end_marker - 1] = '\0'; // replace last character with end-of-string marker.
}
And Iām out of time to keep writing this post, hah⦠Hope this helped!