Hi all,
is it possible to use the length of a string to set a size parameter for an array?
I'm working on building the message for an LED array
Something like this...(which doesn't work)
char displayText[] ="enter text here";
const int msgLen = 6*(strlen(displayText)-1); // miss off the null character from end of char array
// 6 x columns of LEDs per character
boolean message [msgLen][7];
This gives an error "Array bound is not a integer constant"
a little bit of experimentation and further reading...
I see that I don't need the -1 to take the null character into account as strlen will deal with that for me.
So in simple terms, I want to.
set a Char array.
find its length
define a 2D boolean array using the length.
I've included other failed attempts in this code sample..
char displayText[] ="123456789";
// #define msgLen strlen(displayText);
//const int msgLen = strlen(displayText);
const int msgLen = int(strlen(displayText));
boolean message[msgLen][7];
Unless I set the msgLen manually as a number I get the "array bound is not an integer constant" every time
I'm declaring this above the Void loop as I wanted to use a function call (first time I've had a go at one) to populate the Array. Assumed I needed to make it global to keep the contents?
You could declare the string with global scope, and put the boolean inside a function.
char displayText[] ="123456789";
void setup ()
{
// H/w setup in here
// then...
myFn (displayText);
}
void loop ()
{
}
void myFn (char* str)
{
const int msgLen = strlen(str);
boolean message[msgLen][7];
// whatever else you want to do.
// can be done here, put yur own infinite loop
//here and "loop ()" will never be called
}
Can you tell me why this can't be set as a global array?
My plan WAS to have the array configured globally.
Then use a function to fill it with binary depending on the letters in the message. Called from within setup loop.
Then use the loop to repeat the display on an LED matrix.
I can set it all up in a function or in the loop itself but I was trying to be more structured so would be interested to know where I'm going wrong.
It's a good question and one to which I've forgotten the exact answer.
Globals are static so have to have a predetermined size. 'strlen' is a function so can't run outside of another function.
And yes, I do realise that the compiler ought to be able to determine the size of a static string
i guess it boils down to the size of the static allocation must be fixed..