Let me clear the air in case there's any confusion.
strtok will parse a string with more than one token in it with successive calls.
like so:
Code:
char t[] = "12:45:58"
char *p = 0;
p = strtok(t, ":"); // p[...] == "12"
p = strtok(0, ":"); // p[...] == "45"
p = strtok(0, ":"); // p[...] == "58"
And this is why you can't (fully) strtok 2 strings at once
Code:
char t[] = "12:45:58"
char s[] = "1:15:20";
char *p = 0;
p = strtok(t, ":"); // p[...] == "12"
p = strtok(s, ":"); // p[...] == "1";
p = strtok(0, ":"); // p[...] == "15"
p = strtok(0, ":"); // p[...] == "20"
p = strtok(t, ":"); // p[...] == "12"
strtok is also somewhat "global", consider:
Code:
void strTokNull(void)
{ cout << strtok(0, ":") << endl; }
void strTokNew(char *s)
{ cout << strtok(s, ":") << endl; }
int main()
{
char s[] = "12:58:20";
strTokNew(s);
strTokNull();
return 0;
}
if "s" was declared within strTokNew, when we call strTokNull, "s" no longer exists and strtok magically returns null without seg faulting.