Possible errors with this code are:
int* index=(int*)malloc(sizeof(int)*NB);
while(i < NB){
printf("i=%d, pch=%s\n", i, pch);
if (strcmp(pch, SPLIT_PATTERN))
i++;
else
index[k++]=i-1;
pch = strtok(NULL, "-");
}
1)Incorrect value for k
, maybe it's uninitialized, or it goes out of bounds?
2) strtok
is called only with NULL
as parameter. Here is a simple example of how this should work:
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
which gives output
Splitting string "- This, a sample string." into tokens:
This
a
sample
string
For more, read strtok ref.
Note that we do not cast what malloc returns. Why?
[EDIT]
However, your output implies that you are calling strtok
before the while
loop.