I am very much a c newbie and I am learning by following CS107 videos from Standford (I am not a Student there).
Links are below if anyone is interested
Looking at the below implementation of strtok
, I am not sure why the first if statement is written this way: if (s == NULL && ((s = p) == NULL))
char *strtok(char *s, const char *sep)
{
static char *p = NULL;
if (s == NULL && ((s = p) == NULL))
return NULL;
s += strspn(s, sep);
if (!*s)
return p = NULL;
p = s + strcspn(s, sep);
if (*p)
*p++ = '\0';
else
p = NULL;
return s;
}
Here's what I have identified so far:
1) the static
p
is a local static so that it persists once a token is found, and reset to NULL when no more are found2) subsequent calls of
strtoken
pass ins
asNULL
Therefore wouldn't the first check be the same if it were written as:
if (s == NULL && p == NULL)
Or is there a reason it is written the way it is?
Finally: return p = NULL
; is this just a shorthand for:
p = NULL;
return p;