Article View: comp.std.c
Article #2965idle query on strtok()
From: Jim Segrave
Date: Tue, 17 Nov 1992 22:19
Date: Tue, 17 Nov 1992 22:19
44 lines
1890 bytes
1890 bytes
Is strtok()'s behaviour defined for tokens already taken from a string?
For example, suppose I have:
char zoe[] = "aaa bbb";
char *first, *second;
If I do:
first = strtok (zoe, " ");
second = strtok (NULL, " ");
printf ("%s\n%s\n", first, second);
I expect the output:
aaa
bbb
First idle query:
Can I *portably* change the contents of a token returned by strtok
without affecting the behaviour of subsequent calls?
If I do:
first = strtok (zoe, " ");
printf ("%s\n", first);
strcpy (first, "!!!"); /* corrupt the first bit */
second = strtok (NULL, " ");
printf ("%s\n", second);
Do I have a guarantee that I'll get the same output?
What if I change the 'corrupt ...' line to
memset (first, 0, strlen (first) + 1);
Second idle query:
Does strtok() promise that the tokens delivered will be untouched
by subsequent calls to strtok()? Could a conforming implementation
keep the original pointer to the string to be tokenised and use the
string itself to retain state (say by filling from the start of the string
through to the last token delivered with some absurd character)?
I've never met an implementation which did care about previously
delivered tokens or which went back and wantonly trashed them, but I've
never seen any documentation which says that a perverse implementor
couldn't choose to do this.
--
Jim Segrave (Segrave Software Services) jes@grendel.demon.co.uk
Technical Details
Message-ID:
<722063993snx@grendel.demon.co.uk>Path:
arc1-ca.pugleaf.net!sparky!uunet!pipex!demon!grendel.demon.co.uk!jesShow Full Headers
Complete Article Headers
Newsgroups: comp.std.c From: jes@grendel.demon.co.uk (Jim Segrave) Path: arc1-ca.pugleaf.net!sparky!uunet!pipex!demon!grendel.demon.co.uk!jes ReplyTo: jes@grendel.demon.co.uk Subject: idle query on strtok() Distribution: world X-Mailer: cppnews $Revision: 1.19 $ Organization: None Lines: 21 Date: Tue, 17 Nov 1992 22:19:53 +0000 Message-ID: <722063993snx@grendel.demon.co.uk> Sender: usenet@gate.demon.co.uk