I am using strtok to tokenise the string, Is strtok affects the original buffer? For e.g:
*char buf[] = "This Is Start Of life";
char *pch = strtok(buf," ");
while(pch)
{
printf("%s \n", pch);
pch = strtok(NULL," ");
}*
printf("Orignal Buffer:: %s ",buf);
Output is::
This
Is
Start
Of
life
Original Buffer:: This
I read that strtok returns pointer to the next token, then how the buf is getting affected? Is there way to retain original buffer (without extra copy overhead)?
Follow-on Question:: from so far answers I guess there is no way to retain the buffer. So what if I use dynamic array to create original buffer and if strtok is going to affect it, then there will be memory leak while freeing the original buffer or is strtok takes care of freeing memory?