strtok is causing segmentation fault

This is a discussion on strtok is causing segmentation fault within the C Programming forums, part of the General Programming Boards category; The following program is supposed to convert the string into "pig latin." "This is a test string" is supposed to ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    strtok is causing segmentation fault

    The following program is supposed to convert the string into "pig latin." "This is a test string" is supposed to become "hisTay siay aay esttay tringsay." Basically move the first letter to the end and then add "ay." For some reason my program is producing a segmentation fault. Specifically the strtok right after the while condition is what's causing it. I'm not sure why. Is it because I'm passing NULL to the strtok?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      int i;
      char *tokenPtr;
      char string[] = "This is a test string";
    
      tokenPtr = strtok( string, " " );
    
      while ( *tokenPtr != NULL ) {
        tokenPtr = strtok( NULL, " " );
        for ( i = 1; (*(tokenPtr + i) != '\0') && (*(tokenPtr + i) != NULL); ++i ) {
          printf( "%c", *(tokenPtr + i) );
    
          //    printf( "%cay ", *tokenPtr );
        }
        printf( "%cay ", *tokenPtr ); 
      }
    
      printf( "\n" );
    
      return 0;
    }

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    You definately don't enter a for loop if tokenPtr is returned NULL...
    Fix:
    Code:
    while( (tokenPtr = strtok( NULL, " ")) != NULL)
    {
        ....
    }
    Also in the for loop the second condition: (*(tokenPtr + i) != NULL) is not needed and not making sense anyway. A character should not be compared to a pointer, as NULL is...

    And 'i' should start at 0 I suppose.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    182
    To clarify the strtok statement right after the while condition is to move the pointer on to the next word. Without it, the program prints the first word infinitely.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    while ( *tokenPtr != NULL )
    Should be
    while ( tokenPtr != NULL )

    Putting
    tokenPtr = strtok( NULL, " " );
    in the beginning of the loop - you skip the first word

    (*(tokenPtr + i) != NULL) - has no sence left is a char - right is pointer - don't you pay attention to warnings?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Not if you make the loop control statement assign tokenPtr as well as testing whether to enter the loop. I didn't tell you to remove the call to strtok, but place it in a better position.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by vart View Post
    Putting
    tokenPtr = strtok( NULL, " " );
    in the beginning of the loop - you skip the first word
    Yes you are right silly me...
    It should go better like:
    Code:
    tokenPtr = strtok(string, " ");
    if( tokenPtr )
    {
        do 
        {
            ....
        }
        while( (tokenPtr = strtok(NULL, " ") != NULL);
    }
    To handle any case and act on all tokens.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    182
    The do while statement worked well. I don't see why though. Nothing was fundamentally changed as far as I can tell.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by yougene View Post
    The do while statement worked well. I don't see why though.
    And what exactly?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    182
    I guess the better way to put it is I don't see why my original program didn't work. Not including the variable / pointer comparison.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You have *tokenPtr in a loop condition, when tokenPtr is a NULL pointer - dereferencing it causes the crash
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by yougene View Post
    I guess the better way to put it is I don't see why my original program didn't work. Not including the variable / pointer comparison.
    When strtok was done with tokens and returned NULL, your original program happily entered a for loop, and dereferenced the NULL pointer, which caused a segmentation fault.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    182
    Thanks, I'm going to go digest this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting segmentation fault with strtok()
    By Albinoswordfish in forum C Programming
    Replies: 5
    Last Post: 06-10-2009, 04:27 PM
  2. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  3. A libdc1394 library function is causing a segmentation fault
    By Phanixis in forum Linux Programming
    Replies: 7
    Last Post: 10-16-2007, 12:44 PM
  4. strcat causing segmentation fault?
    By jbsloan in forum C Programming
    Replies: 2
    Last Post: 04-02-2005, 09:42 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

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