Strtok function....

can any help me out y dis program is giving me a segmentation fault.....

#include<stdio.h>
#include<string.h>
int main()
{
char *str="Tanvir/home/root/hello";
const char *d ="/";
char *ret;

ret=strtok(str,d);
if(ret==NULL)
printf("NULL NULL");
else
printf("\n%c",ret);

return 0;
}

In this case str points to location that has the initialized value which could be either ready only or read writable.If it happens to be on read only then the strtok function may not be able to modify the string and so generates the SEGV

You could change that to,

char  str[]="Tanvir/home/root/hello";

Thanks
Nagarajan G

Dear Nagarajan,

Thanks for ur detailed explaination of strtok.
I fully agree with ur comment.:b:

but however strtok should not be used in threads. can u explain me why?
they say in such case its safe to use strtok_r().

Regards
-Ashok Meti

Thanks a lot Nagrajan and Ashok !!