开发者

Error in strtok function in C

I am using a simple program to tokenize a string using strtok function. Here is the code -

# include <stdio.h>
char str[] = "now # time for all # good men to # aid of their country";   //line a
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
    printf( "result is \"%s\"\n", result );
    result = strtok( NULL, delims );
}

The program runs successfully. However, if line a is changed to

char * str= "now # time for all # good men to # aid of their country";   //line a 

The strtok function gives a core dump. I would like to get an explanation for my understanding why开发者_StackOverflow社区 this is so ? Because from the declaration of strtok as --char *strtok( char *str1, const char *str2 ); char *str as the first argument should work


char *str = "foo" gives you a pointer to a string literal (you should really be doing const char *, but C allows non-const for backwards compatiblity reasons).

It is undefined behaviour to attempt to modify a string literal. strtok modifies its input.


You can't modify a string literal. The c faq on the subject explains it best. In a nutshell if you declare

char *stuff = "Read-only stuff";

You can't modify it.

The fact that strtok accepts a char * is related to the fact that you can't pass arrays to functions, you can only pass addresses. Another c faq entry might help here.


The previous answers give the required answer, however additional info: You may want to consider using strdup() to create a duplicate of the string, which you CAN then use in strtok(). Just keep a pointer to the original returned buffer, as you'll need to free() it when finished with as it's malloced.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜