I'm coming from a history of hobbyist level BASIC, C#, and Python, and this is my first ground-up embedded project. I'm reasonably sure I know what the problem is but not how to solve it.
An array, defined outside the setup/main loops, is fed into strtok, and is only reading the first result. I assume this is a scope issue that has made the array read only.
char A[255] = "This|is|a|string.";
const char DELIMIT[2] = "|";
char *token;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
}
void loop() {
char B[255] = "Why|not|work";
token = strtok(A, DELIMIT);
while (token) {
Serial.println(token);
token = strtok(NULL, "|");
}
delay(5005);
}
Expected behavior: Serial outputs:
This
is
a
string
once and idles.
Observed behavior: Serial outputs "This" every five seconds (indicating once per loop).
Attempts to repair: I left my local test in the code, and changing A to B in the strtok call functions as expected (although repeats every five seconds, again, as expected). A is, effectively, a pointer, but I attempted to assign a pointer anyway, with identical results.
Copying A to B locally is undesirable as it would create excessive memory usage.
Initializing B as a return from another function is undesirable as this is a response string from a TCP/IP connection and needs to be accessed by multiple functions. I'd also like to keep it defined and not floating on my stack.
Hardware: ESP-8266 12E on a NodeMCU clone. (Known good)
So, obviously the issue is my programming and the scope. Am I missing a good way of doing this, or is using a global going to prevent me from altering the array? It seems to work on another project with an array of bytes, so I'm not ruling out an issue with a strtok edge case, since apparently it calls strtok_r behind the scenes and some people have had issues with that? I've been reading for about six hours over two days, and I'm not finding anything helpful outside my own local variable test.
Thanks.