Thread: C: seg fault: using strtok

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    87

    C: seg fault: using strtok

    Hi members,
    I'm fairly new to C programming. I've worked in python, and bash.

    I want to split string and store it in an array:

    Function takes: character array, and the words in it.

    Code:
    void array_of_str(char *line, unsigned int count){                                                                                                                                             
      unsigned int i=0;                                                                                                                                                                            
      char *array[count];                                                                                                                                                                          
      char *p;                                                                                                                                                                                     
      p=strtok(line,'\t');                                                                                                                                                                         
                                                                                                                                                                                                   
      while(p !=NULL){                                                                                                                                                                             
        printf("Value of I is %d\n",i);                                                                                                                                                            
        array[i++]=p;                                                                                                                                                                              
        p=strtok(NULL,'\t');                                                                                                                                                                       
      }                                                                                                                                                                                            
      printf("##--##\n");                                                                                                                                                                          
    }
    I compile my code as:

    gcc -g -Wall -Wextra *.c -o program_parse -std=gnu99

    I get warnings as:

    Code:
     warning: implicit declaration of function ‘strtok’ [-Wimplicit-function-declaration]
       p=strtok(line,'\t');
    warning: assignment makes pointer from integer without a cast
    p=strtok(NULL,'\t');
    In my headers in main file, I've:

    Code:
    #define _GNU_SOURCE
    #include<string.h>
    When I run my code, i get error:
    Code:
    Segmentation fault (core dumped)
    String I'm passing:
    denovo2819 A15_94875 B13_87655 B23_17316
    count=4

    Unable to solve this.
    Any help would be appreciated.
    thanks.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    #include <string.h> must appear before the use of strtok(). It probably is not included at all (check your headers).

    By the way, "char *array[count]" in your function is unused.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    p=strtok(line,'\t');
    should have been:
    Code:
    p = strtok(line, "\t");
    Notice the second argument.

    You should #include <string.h> in the file containing the definition of array_of_str, not just in the file containing the definition of the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Did you fix those warnings? Running a program that compiled with warnings you don't understand is asking for trouble. Those warnings are very serious problems. The first problem is that you forgot to #include the proper header file that defines strtok(). The second error is related to the first.

    Also I would recommend changing your compile line a little:

    gcc -g -Wall -Wextra -pedantic -pendantic-errors Wvla *.c -o program_parse -std=C99

    This will force gcc to avoid using any compiler specific "features" and use the C99 standard features only.

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    Quote Originally Posted by kmdv View Post
    #include <string.h> must appear before the use of strtok(). It probably is not included at all (check your headers).
    Hi,
    Thanks for your reply.
    <string.h> is included before calling strtok.
    Also,
    denovo2819 A15_94875 B13_87655 B23_17316
    is a character array not string

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    is a character array not string
    But remember strtok() requires a string not an array of char.

    Jim

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by deathmetal
    <string.h> is included before calling strtok.
    No, it isn't: your compiler warning about "implicit declaration of function ‘strtok’" says otherwise.

    Quote Originally Posted by deathmetal
    Also,
    Code:
    denovo2819 A15_94875 B13_87655 B23_17316
    is a character array not string
    What do you mean? Just show an example call of array_of_str, and remember to fix the calls to strtok that I mentioned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    Hi laserlight,

    Thanks for your reply.
    I've
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    #define _GNU_SOURCE
    #include<string.h>
    
    #include "parse_files.h"
    This doesn't fix strtok warning for some reason.

    gcc -v
    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/apps/gcc/4.9.2/libexec/gcc/x86_64-unknown-linux-gnu/4.9.2/lto-wrapper
    Target: x86_64-unknown-linux-gnu
    Configured with: ./configure --prefix=/apps/gcc/4.9.2
    Thread model: posix
    gcc version 4.9.2 (GCC)
    Code:
    char line[100];
    /*
    read file. Get rid of \n
    put '\0' at the end
    
    */
    /*
    get_tab_count - gives you word count
    */
    array_of_str(line,get_tab_count(line)+1);
    Code:
    void array_of_str(char *line, unsigned int count){
      unsigned int i=0;
    
      char *p;
      printf("where are you??\n");
      p=strtok(line,'\t');
    
      while(p !=NULL){
        printf("Value of I is %d\n",i);  
        /*array[i++]=p;*/
        p=strtok(NULL,'\t');
      }
      printf("##--##\n");
    }
    /**/
    Could this be reason:
    c - strtok giving Segmentation Fault - Stack Overflow

    s[]="Test to pass strtok()"; /* this can be passed to strtok() */

    char *m="Test to pass strtok()"; /* passing this will result in SIGSEGV */



    strtok is not being recongized despite adding string.h.
    Why?

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    if I include string.h anywhere else and compile, I get warnings as:

    /usr/include/string.h:348:14: note: expected ‘const char * restrict’ but argument is of type ‘int’
    extern char *strtok (char *__restrict __s, __const char *__restrict __delim)
    ^
    parse_files.c:64:19: warning: passing argument 2 of ‘strtok’ makes pointer from integer without a cast
    p=strtok(NULL,'\t');
    ^
    In file included from parse_files.h:5:0,
    from parse_files.c:4:
    /usr/include/string.h:348:14: note: expected ‘const char * restrict’ but argument is of type ‘int’
    extern char *strtok (char *__restrict __s, __const char *__restrict __delim)

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by deathmetal
    This doesn't fix strtok warning for some reason.
    I don't see the definition of array_of_str, so maybe you got the wrong file.
    Temporarily change this:
    Code:
    void array_of_str(char *line, unsigned int count){
    to:
    Code:
    #include <string.h>
    
    void array_of_str(char *line, unsigned int count){
    Compile. The warning should be gone. You can then move that include to where the includes normally are.

    Quote Originally Posted by deathmetal
    Could this be reason:
    No, because line is a modifiable array.

    It looks like you still have not fixed this:
    Code:
    p=strtok(line,'\t');
    Neither have you fixed this:
    Code:
    p=strtok(NULL,'\t');
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    Hi,
    Thank you for pointing out to the error. Finally it got fixed. Compiler was annoyed with:
    Code:
    p=strtok(line,'\t');
    in stead of:

    Code:
    p=strtok(line,"\t");
    Thanks again for your help and guiding.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Compiler was annoyed with:
    Blaming the compiler for pointing out your programming mistake - whatever next....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    '\t' is not even close to how it sees "\t"

    ' ' is normally only used for char and I'm not even sure how it handles escape and also it does not add a zero byte at the end.
    " " compiler places the string in const segment and add a zero to the end and give you a pointer to the location

    Zero at the end is something strtok() needs as you don't parse for just a char but it expects a (char *) string pointer (even if you just need one byte)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok segmentation fault
    By telmo_d in forum C Programming
    Replies: 2
    Last Post: 10-17-2015, 04:56 PM
  2. strtok segmentation fault
    By aboosoyeed in forum C Programming
    Replies: 4
    Last Post: 03-31-2012, 10:51 AM
  3. Getting segmentation fault with strtok()
    By Albinoswordfish in forum C Programming
    Replies: 5
    Last Post: 06-10-2009, 04:27 PM
  4. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread