strtok segmentation fault

The problem is that you’re attempting to modify a string literal. Doing so causes your program’s behavior to be undefined. Saying that you’re not allowed to modify a string literal is an oversimplification. Saying that string literals are const is incorrect; they’re not. WARNING : Digression follows. The string literal “this is a test” is of an expression of type char[15] (14 … Read more

Implementation of strtok() function

Internal implementation of strtok has already been discussed here: How does strtok() split the string into tokens in C? In your type of implementation ( calling it your type because it is quite different from the actual one), you have not allocated any memory dynamically to the local variable ‘W’. So when you return it, … Read more

How does the strtok function in C work? [duplicate]

I found this sample program which explains the strtok function: However, I don’t see how this is possible to work. How is it possible that pch = strtok (NULL, ” ,.-“); returns a new token. I mean, we are calling strtokwith NULL . This doesen’t make a lot sense to me.

C: correct usage of strtok_r

The documentation for strtok_r is quite clear. The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string. On the first call to strtok_r(), str should point to the string … Read more