Something wrong with function

This is a discussion on Something wrong with function within the C Programming forums, part of the General Programming Boards category; Hi I am sorry if this is trivial, i've used php before C and i've got old habits. I've got ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Question Something wrong with function

    Hi

    I am sorry if this is trivial, i've used php before C and i've got old habits. I've got a function that gives me segmentation fault in return. Here it is:

    Code:
    int check_user ( char *name ) {
            char *token=NULL;
            char check[255];
            sprintf(check,"%s",nocheck);
            token=strtok(check,",");
            if (strcmp(token, name) == 0) { return 1; }
            while (token!=NULL) {
                    token=strtok(NULL,",");
                    if (strcmp(token, named) == 0) { return 1; }
            }
            return 0;
    }
    name is a short string, nocheck(it's a constant) is a string with some words delimited with a ",".
    Thing that messes it all up is this line:
    while (token!=NULL) {

    I don't know how to do it as all pages with strtok() functions that i have foun with an example is similar to what i have wrote...

    Could You please tell me what do i do wrong?
    And sorry for my crapy english..

    Best regards

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The problem is that when strtok returns NULL, you use that return value before ever checking it. Do it like this:
    Code:
    while((token = strtok(NULL, ",")) != NULL)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Thank You for an answer but still...it gives me the same error...

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Use the debugger to step through the code line by line. Watch the variable values as they change. If you don't figure it out from that, report your findings here.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I would suggest something more like this.
    Code:
       token = strtok(check, ",");
       while ( token != NULL )
       {
          if ( strcmp(token, name) == 0 )
          {
             return 1;
          }
          token = strtok(NULL, ",");
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Thank You very much!
    That's it

    It works just fine.

    Best regards!

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I'm not a strtok fan, so I might really prefer to write it something more like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int found_user ( const char *name, const char *text, const char *delim )
    {
       const char *token = text; /* point to the beginning of the text */
       for ( ;; )
       {
          size_t len = strcspn(token, delim); /* look for the delimiter(s) */
          if ( strncmp(token, name, len) == 0 ) /* compare result with 'name' */
          {
             return 1; /* found 'name' */
          }
          token += len; /* advance pointer by the length of the delimited text */
          if ( *token == '\0' )
          {
             break; /* advanced to the terminating null character */
          }
          ++token; /* skip the delimiter */
       }
       return 0; /* searched all of 'text', did not find 'name' */
    }
    
    void foo ( const char *name, const char *text, const char *delim )
    {
       printf("found_user(\"%s\", \"%s\", \"%s\") = %d\n",
              name, text, delim, found_user(name, text, delim));
    }
    
    int main(void)
    {
       static const char line1[] = "one,two,three";
       static const char line2[] = "one two three";
       static const char name1[] = "two";
       static const char name2[] = "four";
       static const char comma[] = ",";
       static const char space[] = " ";
       foo(name1, line1, comma);
       foo(name2, line1, comma);
       foo(name1, line2, comma);
       foo(name1, line2, space);
       return 0;
    }
    
    /* my output
    found_user("two", "one,two,three", ",") = 1
    found_user("four", "one,two,three", ",") = 0
    found_user("two", "one two three", ",") = 0
    found_user("two", "one two three", " ") = 1
    */
    Last edited by Dave_Sinkula; 01-03-2006 at 09:23 AM. Reason: Added intermediate function to trim page width.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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