strtok segmentation fault

This is a discussion on strtok segmentation fault within the C Programming forums, part of the General Programming Boards category; Can someone point out the mistake . I thought this was the crrect way of using strtok. But its running ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    15

    strtok segmentation fault

    Can someone point out the mistake . I thought this was the crrect way of using strtok. But its running into seg fault

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    int main()
    {
        char * tch=strtok("aaaa its okay"," ");
        while (tch != NULL)
        {
            printf("%s\n",tch);
            tch = strtok (NULL, " ");
        }
        
     }


    gdb output
    gdb) run
    Starting program: /var/www/YB_EmailAlerts/test/a.out


    Program received signal SIGSEGV, Segmentation fault.
    0xb7ebd0c5 in strtok () from /lib/i386-linux-gnu/libc.so.6
    (gdb) up
    #1 0x08048432 in main () at a.c:9
    9 char * tch=strtok("aaaa its okay"," ");

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    strtok needs to be able to write to the string that you're tokenizing (it writes over the ending character with a '\0'). Literal strings are often not writable. So try
    Code:
    str [] = "aaaa its okay";
    char * tch=strtok(str," ");
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    thats right@oogabooga... so @aboosoyeed modify that and see what happens and tell me..

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by Nyah Check View Post
    thats right@oogabooga... so @aboosoyeed modify that and see what happens and tell me..
    Don't post if you have nothing to say.

    And try to make your posts conform to some type of grammar, they become twice as annoying if you don't.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    hmmmmmm

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. Segmentation Fault
    By Terran in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2008, 07:39 PM
  3. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09:32 AM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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