advanced patterns When Using `strtok` in a Loop with Global String in C
I'm writing unit tests and I'm running into an scenario with the `strtok` function when trying to tokenize a global string in a loop. I have a function that should parse a comma-separated list of values, but I get inconsistent results when I call `strtok` multiple times on the same string. Here's my code snippet: ```c #include <stdio.h> #include <string.h> char global_input[] = "apple,banana,orange,grape"; void parse_input() { char *token; token = strtok(global_input, ","); while (token != NULL) { printf("Token: %s\n", token); token = strtok(NULL, ","); } } int main() { parse_input(); // Trying to parse again parse_input(); return 0; } ``` The output on the first call to `parse_input` is as expected: ``` Token: apple Token: banana Token: orange Token: grape ``` However, when I call the `parse_input` function a second time, I get no output. I was expecting it to parse the string again. I understand that `strtok` modifies the original string, but I thought that since I'm calling `parse_input` as a separate function, I could reuse the global string. Am I misunderstanding how `strtok` works, or is there something else going on? I also tried resetting `global_input` before the second call, but it didn't change the outcome. Any insight into why this is happening would be greatly appreciated. I'm using C 3.9 in this project. My team is using C for this CLI tool. I'm open to any suggestions.