Understading how strtok works...

This is a discussion on Understading how strtok works... within the C Programming forums, part of the General Programming Boards category; I've found some strtok example on the net, which is this: http://www.phim.unibe.ch/comp_doc/c_...MPLES/strtok.c I can easly understand the code and I've ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    158

    Understading how strtok works...

    I've found some strtok example on the net, which is this: http://www.phim.unibe.ch/comp_doc/c_...MPLES/strtok.c

    I can easly understand the code and I've already adapted it to my own code, however, there's something I don't understand, which is this line (more exactly the thing in bold):

    while ( (sub_string=strtok(NULL, " ")) != NULL)

    How come strtok() will get the other strings out of test_string if only I have there in the function parameter is NULL? Where does he get the data from?

    Just want to understand that...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > Where does he get the data from?
    The first time you call it, you pass the pointer to the string you want to tokenise.
    After that, it remembers where it got to all by itself, and returns each successive token.

    This also makes it much harder to use strtok() to tokenise two strings in parallel.

    Not to mention that strtok() also modifies the string in the process, which comes as a shock to the average newbie.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>Not to mention that strtok() also modifies the string in the process, which comes as a shock to the average newbie.

    [rant]It shouldn't be shocking if people would learn to read the manual (RTFM)! or read their text books. But some newbes prefer to shoot from the hip and toss together a program using functions they have no clue what they do.[/rant]

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    lol... actually, after playing with it and seeing that example I think I got how it work and as I said before, my program woks pretty good with it, the way I code it. I only didn't understand that little thing that you have now clarified for me

    Thank won't change anything in my app cause I wasn't having any trouble, I just wanted to know how it was getting the string.

    thanks

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It shouldn't be shocking if people would learn to read the manual (RTFM)! or read their text books.
    I wasn't aware that the average newbie did such things.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. a question about strtok.
    By Masterx in forum C++ Programming
    Replies: 24
    Last Post: 11-18-2008, 10:09 PM
  3. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  4. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  5. strtok with "" as delimiter?
    By fanoliv in forum C Programming
    Replies: 15
    Last Post: 06-19-2006, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21