xu li wrote:
> i want to use apr_strtok to split the string.but it failed.
>
> my_test.c:
> #include <stdio.h>
> #include "apr_strings.h"
> int main(void)
> {
> char *strToSplit = "Hello world";
> char *last;
> char *strResult;
>
> strResult = apr_strtok(strToSplit," ",&last);
>
> printf("%s\n",strResult);
>
>
> return 0;
> }
> it failed at apr_strtok.c line 51 **last = '\0';
> The gdb infomation :
> Program received signal SIGSEGV, Segmentation fault.
> apr_strtok (str=0x80485d8 "Hello world", sep=0x80485e4 " ", last=0xbfff7740)
> at strings/apr_strtok.c:51
> 51 **last = '\0';
>
> could someone tell me the reason?
> Thanks in advance.
apr_strtok changes the string as it finds tokens. It inserts NULLs into the
string.
This may not work on a string constant like "Hello world".
Try this to make strToSplit a string which can be modified by apr_strtok:
#include <stdio.h>
#include "apr_strings.h"
int main(void)
{
char strToSplit[32];
char *last;
char *strResult;
strcpy(strToSplit, "Hello world");
strResult = apr_strtok(strToSplit," ",&last);
printf("%s\n",strResult);
return 0;
}