+ 1

[Solved] Could anyone spot out the error in my code? Search results yield unrelated stuff.

Basically, my code (which tries to exactly mimic strtok from <string.h>) isn't working alright. Any suggestions would be appreciated: https://www.sololearn.com/en/compiler-playground/cvC847qh9sll (Code Modified)

9th Sep 2024, 4:58 AM
Calvin Jude
Calvin Jude - avatar
3 Réponses
+ 6
Calvin Jude to initialize a static variable within a function, the compiler has to determine its value at compile time. The parameter, str, is passed on the stack at runtime so the compiler is unable to determine what initial value to assign. To mimic strtok() you will need to check for a pointer being passed at runtime. static char *last; if (str) last = str; This fixes the problem and makes it work just like the library version.
9th Sep 2024, 8:20 AM
Brian
Brian - avatar
9th Sep 2024, 5:17 AM
Bob_Li
Bob_Li - avatar
+ 1
Brian Thanks for the concise answer! Much appreciated
10th Sep 2024, 3:22 AM
Calvin Jude
Calvin Jude - avatar