Hi Guys,
I had this code working earlier but, somehow I changed something. I trimmed the code back all the way to the very basics.
int len = 14;
char data[len];
void setup() {}
void loop() {}
The sketch compile error is:
sketch_jan13a:3:14: error: array bound is not an integer constant before ']' token
char data[len];
^
exit status 1
array bound is not an integer constant before ']' token
The problem is obvious to someone but, I don't get it. I read this post from a year ago and I don't get it. Thanks for any insight.
The error message is clear. Try this
const int len = 14;
char data[len];
Thank you. That worked. I almost tried that but, I felt that I should understand the compile error that I was getting. "not an integer constant" just wasn't clear enough for me I guess.
The prior code in a nut shell was:
byte len = whatIsWritten.length();
// some code
char data[len+1];
The above works without 'const'. Anyway, problem solved. Thank you very much.
I doubt that. Local array variables can be temporarily defined like that, valid ONLY within a function or block, but not for the main program.
It's because the compiler allocates fixed memory areas for arrays at compile time. If the size of the array is a variable, how could it do that? Either
- Changing the value of the variable would violate that principle if it is used to specify the array size, risking accesses outside of the array bounds
or
- Allowing arrays to change size dynamically would dramatically complicate the allocation of memory (but in C++ STL those kinds of arrays do exist).
And even that is dodgy, i.e., not allowed in ISO C++.