You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by xu li <ze...@yahoo.com.cn> on 2007/03/21 09:23:08 UTC

function apr_strtok does not work..

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.
 		
---------------------------------
 Mp3疯狂搜-新歌热歌高速下   

Re: function apr_strtok does not work..

Posted by Tom Donovan <do...@bellatlantic.net>.
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;
}


Re: function apr_strtok does not work..

Posted by Lucian Adrian Grijincu <lu...@avira.com>.
I'm not sure if this is the cause, but you must call apr_initialize() or
 apr_app_initialize() before you use APR.

http://apr.apache.org/docs/apr/trunk/group__apr__library.html


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;
> 
	apr_initialize();
	atexit(apr_terminate);
>         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.
> 
> ------------------------------------------------------------------------
> Mp3疯狂搜-新歌热歌高速下
> <http://music.yahoo.com.cn/?source=mail_mailbox_footer>