char *s = "any string"
is the definition of a pointer pointing to a string or char (s) array. in the above example, s points to a constant string
char s[] = "any string"
is the definition of the char (s) array. in the above example, s is an array of char (s) that contains charcters {'a','n','y',' ','s','t','r,'i','n','g','\0'}
strtock modifies the contents of the input string. it replaces the delimiters in your string with '\0' (null).
So you cannot use strtok with constant lines like this:
char *s="any string"
you can use strtok with dynamic memory or static memory:
char *s = malloc(20 * sizeof(char)); //dynamic allocation for string strcpy(s,"any string"); char s[20] = "any string"; //static allocation for string char s[] = "any string"; //static allocation for string
source share