I have the following code to split strings by tokens:
char **strToWordArray(char *str, const char *delimiter)
{
  char **words;
  int nwords = 1;
  words = malloc(sizeof(*words) * (nwords + 1));
  int w = 0;
  int len = strlen(delimiter);
  words[w++] = str;
  while (*str)
  {
    if (strncmp(str, delimiter, len) == 0)
    {
      for (int i = 0; i < len; i++)
      {
        *(str++) = 0;
      }
      if (*str != 0) {
        nwords++;
        char **tmp = realloc(words, sizeof(*words) * (nwords + 1));
        words = tmp;
        words[w++] = str;
      } else {
          str--;
      }
    }
    str++;
  }
  words[w] = NULL;
  return words;
}
If I do this:
char str[] = "abc/def/foo/bar";
char **words=strToWordArray(str,"/"); 
then the program works just fine but if I do this:
char *str = "abc/def/foo/bar";
char **words=strToWordArray(str,"/");
then I get a segmentation fault.
Why is that? The program expects a char* as an argument then why does a char* argument crash the program?
 
    