So I found a bit of code over in stackoverflow that I found a bit difficult to understand. What it does is it splits a string based on a delimiter and then stores the rest in a pointer while returning the part that was split.
Here it is
Code:
char *strtok_r(char *str, const char *delim, char **nextp)
{
char *ret = NULL;
if (str == NULL)
{
str = *nextp;
}
str += strspn(str, delim); // This part I don't understand
if (*str == '\0')
{
return NULL;
}
ret = str;
str += strcspn(str, delim); // and this part
if (*str)
{
*str++ = '\0';
}
*nextp = str;
return ret;
}
what strspn does is returns the length of the initial portion of str1 containing only characters that appear in str2 and strcspn does the opposite. The function's return type is size_t and not something like char *. So how is it that at the end of this function it is able to get the delimited part of the string based on the length that it got from strspn and strscpn?