Segmentation Fault when using strtok_r

后端 未结 6 985
野的像风
野的像风 2020-12-14 02:55

Can anyone explain why I am getting segmentation fault in the following example?

#include 
#include 

int main(void) {
  char          


        
6条回答
  •  天命终不由人
    2020-12-14 03:31

    You have understood the usage of strtok_r incorrectly. Please check this example and documentation

    And try & see this:

    #include 
    #include     
    
    int main(void)
    {
        char hello[] = "Hello World, let me live.";
    
        char *tmp;
        char *token = NULL;
        for(token = strtok_r(hello, ", ", &tmp);
            token != NULL;
            token = strtok_r(NULL, ", ", &tmp))
        {
            printf("%s\n", token);
        }
    
        return 0;
    }
    

提交回复
热议问题