Thread: strtok()

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    46

    strtok()

    I was reading The C Programming Language by Kernighan and Ritchie. I noticed a reference to a function in the standard library header string.h that I might be able to find a lot of uses for in my programs, strtok().
    Could someone show me a bit of code describing how this function works, as my book doesn't say how to use it, only what the function declaration is and a description of what it does.

    -CeeCee
    C code. C code run. Run code, run...please!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's amazing what a board search for the word strtok can produce...

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    Okay, point made. But I still have a few questions
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
      char test_msg[]="This is a test message";
      char *p;
      char *delim = " ";
      char *words[20];
      int i = 0, j;
    
      p = strtok(test_msg,delim);
      while ( p ) {
        words[i] = p;
        p = strtok(NULL,delim);
        i++;
      } 
    
      printf ( "%d\n", i );
      for ( j = 0 ; j < i ; j++ ){
        printf("'%s'\n", words[j]);
      }
    }
    p = strtok(NULL,delim);
    What does this do and why? I know that it assigns a token to the char pointer p, and that spaces don't count as tokens, but what is NULL there for?
    C code. C code run. Run code, run...please!

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I think the first call to strtok returns a pointer to the first word ("This" in this case). The subsequent calls to NULL force the pointer up the string to the next token without reassigning it to anywhere else.

    I think anyway.....

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I think the first call to strtok returns a pointer to the first word
    > ("This" in this case). The subsequent calls to NULL force the
    > pointer up the string to the next token without reassigning it to
    > anywhere else.

    Yup. Good explaination. For another description, try here.

    I like the comment:

    "Never use this function."

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    Never use this function? I find that a bit funny because if it weren't meant to be used, why have it? Besides, if I wanted to extract tokens from a string then the last thing I would want is to leave the string unchanged. Even if for some strange reason I did I could copy the string and change the copy.

    Is that just the opinion of the person who wrote that?

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    All you have to remember about strtok is that if you need to keep the original data then tokenise a copy. The modification of the original data is the only downside to strtok.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

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, 05:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM