问题
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?
回答1:
strtok()
doesn't create a new string and return it; it returns a pointer to the token within the string you pass as argument to strtok()
. Therefore the original string gets affected.
strtok()
breaks the string means it replaces the delimiter
character with NULL and returns a pointer to the beginning of that token. Therefore after you run strtok()
the delim
characters will be replaced by NULL characters. You can read link1 link2.
As you can see in output of example in link2
, the output you are getting is as expected since the delim
character is replaced by strtok
.
回答2:
When you do strtok(NULL, "|")
, strtok
finds a token and puts null on place (replace delimiter with '\0'
) and modifies the string. So you need to make the copy of the original string before tokenization.
Please try following:
void main(void)
{
char buf[] = "This Is Start Of life";
char *buf1;
/* calloc() function will allocate the memory & initialize its to the NULL*/
buf1 = calloc(strlen(buf)+1, sizeof(char));
strcpy(buf1, buf);
char *pch = strtok(buf," ");
while(pch)
{
printf("%s \n", pch);
pch = strtok(NULL," ");
}
printf("Original Buffer:: %s ",buf1);
}
来源:https://stackoverflow.com/questions/23192362/strtok-affects-the-input-buffer