Re: Why occur the mistake in runtime about strtok()?



hu wrote:

the strtok() declaration:
char * strtok(char *p1, const char*p2);
The strtok() can overwrite *p1?
And what it overwirte the content of p1 with?

You seem to have no idea of what strtok does.

N869
7.21.5.8 The strtok function
Synopsis
[#1]
#include <string.h>
char *strtok(char * restrict s1,
const char * restrict s2);
Description
[#2] A sequence of calls to the strtok function breaks the
string pointed to by s1 into a sequence of tokens, each of
which is delimited by a character from the string pointed to
by s2. The first call in the sequence has a non-null first
argument; subsequent calls in the sequence have a null first
argument. The separator string pointed to by s2 may be
different from call to call.
[#3] The first call in the sequence searches the string
pointed to by s1 for the first character that is not
contained in the current separator string pointed to by s2.
If no such character is found, then there are no tokens in
the string pointed to by s1 and the strtok function returns
a null pointer. If such a character is found, it is the
start of the first token.
[#4] The strtok function then searches from there for a
character that is contained in the current separator string.
If no such character is found, the current token extends to
the end of the string pointed to by s1, and subsequent
searches for a token will return a null pointer. If such a
character is found, it is overwritten by a null character,
which terminates the current token. The strtok function
saves a pointer to the following character, from which the
next search for a token will start.
[#5] Each subsequent call, with a null pointer as the value
of the first argument, starts searching from the saved
pointer and behaves as described above.
[#6] The implementation shall behave as if no library
function calls the strtok function.
Returns
[#7] The strtok function returns a pointer to the first
character of a token, or a null pointer if there is no
token.
[#8] EXAMPLE 1
#include <string.h>
static char str[] = "?a???b,,,#c";
char *t;
t = strtok(str, "?"); // t points to the token "a"
t = strtok(NULL, ","); // t points to the token "??b"
t = strtok(NULL, "#,"); // t points to the token "c"
t = strtok(NULL, "?"); // t is a null pointer


--
pete
.



Relevant Pages

  • Re: strtok ( ) help
    ... > splitCommandssomehow modifying the pointer, but I HAVE to call that ... Here's an idea of how to use the strtok() function. ... don't mind trashing the contents of a string s, ... will give you a loop that extracts the tokens one at a time from s. ...
    (comp.lang.c)
  • Re: use of strtok( )
    ... to strtok. ... The second argument is a list of delimiter characters - any one of ... null character, strtok has a note of the location of the third "?" ... On this call, as the first argument is a NULL pointer, strtok ...
    (comp.lang.c)
  • Re: Parsing options in the same way they are passed to main
    ... Use strtok() to get each token in turn, and add each (pointer ... int main ... "Ain't I'm a dog?" ...
    (comp.lang.c)
  • Re: parsing commands and parameters
    ... things like embedded quotation marks - is to use a proper lexical analyzer (aka "lexer") and parser. ... The parser recognizes a sequence of tokens and acts on it, ... This is where we will manipulate strtok() to our ...
    (comp.lang.c)
  • Re: Free memory cause segmentation fault.
    ... Have you read and understood the operation of the strtok() function? ... Are you aware that it overwrites the character terminating each token ... and you will allocate 5 bytes for ... Writing past the end of allocated memory produces undefined behavior, ...
    (comp.lang.c)