Nested strtok

This is a discussion on Nested strtok within the C Programming forums, part of the General Programming Boards category; Consider this loop: Code: while (t != NULL) { t = strtok(NULL, tbc); if (t != NULL) { if(time_check_legal(t)) row_add_key(_row, ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    30

    Nested strtok

    Consider this loop:

    Code:
    	    while (t != NULL)
    	    {
    			t = strtok(NULL, tbc);
    			if (t != NULL)
    			{
    				if(time_check_legal(t)) row_add_key(_row, t);  // <--- MESSES UP
    
    			}
    	    }
    The function time_check_legal(const char *_time) also uses strtok(), twice, and it seems it messes up the strtok loop. Is this normal ? Is there a workaround ?

    Best wishes, Des

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    strtok is not reentrant. There may be a strtok_r.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Remember that strtok modifies the string it is parsing for the delimeter. if time_check _legal is parsing the same string you may put your self in a bind. You should copy the string first.


    For more help:
    http://www.gnu.org/software/libtool/...-a-String.html


    Also your strtok call against NULL(string) is confusing.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by Dave_Sinkula View Post
    strtok is not reentrant. There may be a strtok_r.
    Thanks Dave, what I was wanting to say.

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. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  3. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM

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