strtok query

Discussion in 'C' started by rajib, May 26, 2007.

  1. rajib

    rajib New Member

    Joined:
    May 18, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    When strtok is used and it doesn't find the delimiter when strtok is used the FIRST time, it should return a null. But this doesn't seems to be the behavior. Pl refer to this code -

    Code:
    char*  szInputStr = "string to split up";
    char *copy = NULL;
    
    /* Copy the szInputStr to Copy */
    
    len = strlen(szInputStr);
    copy = (char *)malloc(len+1);
    strcpy(copy, szInputStr);
    
    /* Do strtok */
    result = strtok(copy, "~");
    if(result == NULL)
    printf("result is NULL \n");
    else
    printf("result is NOT null \n");
    The above code should return NULL - right? Please confirm.

    Actually the value is same as the original string - "string to split up". The above code prints -
    "result is NOT null"
     
    Last edited by a moderator: May 26, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    No, it should not. Strtok returns a NULL when no more tokens are found. On the first call it found "string to split up". The return will be a pointer to that token, which happens to be the entire input string.

    Do yourself a favor: read the documentations for the things you use.

    Do us a favor: learn to use the code tags like a big boy. Quit forcing the task on Shabbir.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice