Thread: Having difficulty understanding strtok()

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Having difficulty understanding strtok()

    I am using strtok() for the first time to tokenize a string from a file. From checking, I can sucessfully open then file and read the whole string from the file and input it into a string variable.

    What I am having trouble with is tokenizing the string properly and storing it into a 2d string.

    When I check by testing each iteration, I "print" through ncurses the same thing over and over agian. It seems that I am able to tokenize the first section of the string. However it seems that I cannot tokenize the next part of the string when moving to the next iteration.

    Code:
      /* iteration counter 1 */
      int counterOne;
      
      /* room value 2 dimensional string variable and initialization of first
      dimension*/ 
      char * * roomValue = malloc(sizeof(char *) *151);
      
      /* iteration to initailize second dimension */
      for(counterOne = 0; counterOne != 151; counterOne++)
      {
        roomValue[counterOne] = malloc(sizeof(char *) *151);
      }
    
      /* token string variable */
      char * token = malloc(sizeof(char *) * 151);
    
      /* parsing of file input sting string by iterarations */
      for(counterOne = 0; counterOne != 151; counterOne++)
      {
        /* tokenize by iteration */
        token = strtok(fileInput, " ");
        
        /* copy token to room value string */
        strcpy(roomValue[counterOne], token);
       
        /* test per iteration */ 
        mvaddstr(counterOne, 0,roomValue[counterOne]);
      }
    The only things I can think of is that:
    a) I am not using strtok() correctly,
    b) I am not coping the token to the 2d string correctly,
    c) The logic that I am using is wrong?

    I would really apprecaite some insight as to what the problem is. Also, thanks for taking time to help ahead of time!
    Last edited by navitude; 03-01-2013 at 01:28 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Successive calls of strtok() on the same string should be passed NULL instead of the file-pointer again. If you pass a file-pointer, then the last location which strtok saves is replaced with the new.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to study the following link: strtok() and notice that you don't need to allocate memory for the pointer used in strtok(). Your present code has a memory leak because of this issue. Also for now you may want to forgo the dynamic memory and use statically allocated arrays until you have mastered strtok().

    Jim

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    char * * roomValue = malloc(sizeof(char *) *151);
      
    /* iteration to initailize second dimension */
    for(counterOne = 0; counterOne != 151; counterOne++)
    {
        roomValue[counterOne] = malloc(sizeof(char *) *151);
    }
    
    /* token string variable */
    char * token = malloc(sizeof(char *) * 151);
    "roomValue[counterOne]" is a pointer to char. Therefore you would need to allocate memory for 151 chars and not for 151 pointers to char. Same for "token". In your case you are lucky and sizeof(char*) is probably always bigger than sizeof(char) and you just allocate too much memory.

    To avoid this you should use the idiom:
    Code:
    Type *var = malloc(sizeof(*var) * n);
    // for example:
    int *a = malloc(sizeof(*a));
    double *b = malloc(sizeof(*b) * 10);
    struct some_struct *c = malloc(sizeof(*c) * 50);
    where "Type" is any type, "n" the number of elements (if needed) and sizeof(*var) will result in the correct size for the object "var" points to. This has the advantage that you can change the type whenever you want to and the compiler will still be able to determine the correct size.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding recursion by parts difficulty
    By zone159 in forum C Programming
    Replies: 2
    Last Post: 12-14-2012, 09:22 PM
  2. [RNG] Progressive difficulty
    By Mario F. in forum General Discussions
    Replies: 8
    Last Post: 03-17-2010, 09:35 PM
  3. Understanding strtok from string.h library
    By yougene in forum C Programming
    Replies: 6
    Last Post: 03-07-2008, 01:14 AM
  4. got difficulty on this
    By gtr_s15 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2006, 09:37 AM
  5. Game difficulty
    By pdstatha in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2002, 04:12 PM