In the second example:
char *string = "hello world";
char *result = strtok(string, " ");
the pointer string
is pointing to a string literal, which cannot be modified (as strtok()
would like to do).
You could do something along the lines of:
char *string = strdup("hello world");
char *result = strtok(string, " ");
so that string
is pointing to a modifiable copy of the literal.