You are not logged in.
char *
strtok (s, delim)
char *s;
const char *delim;
{
char *token;
if (s == NULL)
s = olds;
/* Scan leading delimiters. */
s += strspn (s, delim);
if (*s == '\0')
{
olds = s;
return NULL;
}
/* Find the end of the token. */
token = s;
s = strpbrk (token, delim);
if (s == NULL)
/* This token finishes the string. */
olds = __rawmemchr (token, '\0');
else
{
/* Terminate the token and make OLDS point past it. */
*s = '\0';
olds = s + 1;
}
return token;
}
i don't understand bolded line: s += strspn (s, delim);
the function returns a size_t type wile s is a chr* type from invoking strtok by user,
if compiling this, no warning?
thks!
Last edited by neoanima (2011-06-09 10:45:59)
Offline
Note the '+=' operator.
strspn returns the length of the beginning of the string s that contains only characters contained in delim.
For instance if s = " frobincate" (2 leading spaces) and delim = " \t\n" then strspn would return 2; as the first 2 characters of s (more techincally, the string that s points to) are contained in delim, but the 3rd character 'f' is not. After the line s += strspn(s, delim) is executed, s would be incremented to point to the 'f'. In other words, after the statement, s would be 'frobincate",
Last edited by rockin turtle (2011-06-08 04:18:41)
Offline
In C, "s += n" means "s = s + n". If s is a pointer, "s + n" means "the address n objects after s," or "&s[n]". Actually, "s[n]" directly translates to "*(s + n)", which is why you can write "n[ s]" for the same effect (although don't because it's confusing).
Offline
ah, i see. with warm-hearted guys like you, it is easier to learn programming.
now i know how this statement move the pointer to the first char not in the delim.
thanks!
Last edited by neoanima (2011-06-08 08:25:08)
Offline
You're welcome. Please mark the thread [SOLVED].
Offline