strtok problem

Discussion in 'C Programming' started by jorntk@yahoo.com, Aug 28, 2003.

  1. Guest

    #include <stdio.h>
    #include <string.h>

    void strlines(char line[], char tokens[])
    {
    char * token_ptr, token[81];
    token_ptr = strtok(line, " ");
    while(token_ptr)
    {
    strcpy(token,token_ptr);
    strcat(tokens, strcat(token, "\n"));
    token_ptr=strtok(NULL, " ");
    }
    }
    int main()
    {
    char sentence[81], words[81];
    printf("enter a sentence: ");
    fgets(sentence, sizeof sentence, stdin);
    strlines(sentence,words);
    puts(words);
    }

    why the above code display ,X @ at the beginning of the output?
     
    , Aug 28, 2003
    #1
    1. Advertising

  2. Ben Pfaff Guest

    () writes:

    > void strlines(char line[], char tokens[])


    This function appends to tokens[], but doesn't initialize it.

    > int main()
    > {
    > char sentence[81], words[81];
    > printf("enter a sentence: ");
    > fgets(sentence, sizeof sentence, stdin);
    > strlines(sentence,words);


    This functions passes words[] to strlines() without initializing
    it.

    > puts(words);


    You should return a value from main().

    > }
    >
    > why the above code display ,X @ at the beginning of the output?


    Is it really surprising given the failure to initialize?
    --
    "When in doubt, treat ``feature'' as a pejorative.
    (Think of a hundred-bladed Swiss army knife.)"
    --Kernighan and Plauger, _Software Tools_
     
    Ben Pfaff, Aug 28, 2003
    #2
    1. Advertising

  3. On 28 Aug 2003 00:24:23 -0700, Ben Pfaff <> wrote:

    > () writes:
    >
    >> void strlines(char line[], char tokens[])

    >
    >This function appends to tokens[], but doesn't initialize it.
    >
    >> int main()
    >> {
    >> char sentence[81], words[81];
    >> printf("enter a sentence: ");
    >> fgets(sentence, sizeof sentence, stdin);
    >> strlines(sentence,words);

    >
    >This functions passes words[] to strlines() without initializing
    >it.


    But words is converted to a pointer to the first element of the array
    so there is no reference at this point to an uninitialized variable.

    >
    >> puts(words);

    >
    >You should return a value from main().
    >
    >> }
    >>
    >> why the above code display ,X @ at the beginning of the output?

    >
    >Is it really surprising given the failure to initialize?




    <<Remove the del for email>>
     
    Barry Schwarz, Aug 29, 2003
    #3
  4. On 27 Aug 2003 23:17:19 -0700, ()
    wrote:

    >#include <stdio.h>
    >#include <string.h>
    >


    You shouldn't use str as the prefix for a function name you are
    defining. It is reserved.

    >void strlines(char line[], char tokens[])
    >{


    At this point tokens, which is treated as a pointer in this context,
    points to the first element of words in main. Unfortunately, that
    element has not yet been initialized.

    > char * token_ptr, token[81];
    > token_ptr = strtok(line, " ");
    > while(token_ptr)
    > {
    > strcpy(token,token_ptr);
    > strcat(tokens, strcat(token, "\n"));


    strcat requires that the first argument (the destination) point to a
    well formed string. tokens does not. I points to the uninitialized
    array words in main.

    You can initialize words where it is defined in main, by code in main,
    or by initializing tokens[0] to '\0' in strlines.

    > token_ptr=strtok(NULL, " ");
    > }
    >}
    >int main()
    >{
    > char sentence[81], words[81];
    > printf("enter a sentence: ");
    > fgets(sentence, sizeof sentence, stdin);
    > strlines(sentence,words);
    > puts(words);
    >}
    >
    >why the above code display ,X @ at the beginning of the output?


    You were unlucky. On a good day, the first call to strcat would have
    caused your program to abort. Basically, strcat started looking for a
    '\0' and eventually found one just a couple of bytes from the tokens
    was pointing. The very nature of strcat caused these uninitialized
    bytes to remain unchanged during the life of your program.



    <<Remove the del for email>>
     
    Barry Schwarz, Aug 29, 2003
    #4
  5. pete Guest

    Barry Schwarz wrote:
    >
    > On 27 Aug 2003 23:17:19 -0700, ()
    > wrote:
    >
    > >#include <stdio.h>
    > >#include <string.h>
    > >

    >
    > You shouldn't use str as the prefix for a function name you are
    > defining. It is reserved.


    > >void strlines(char line[], char tokens[])
    > >{


    "str" followed by an underscore would be OK:
    str_lines()

    --
    pete
     
    pete, Aug 29, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ram Laxman

    strtok problem

    Ram Laxman, Apr 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    5,102
    Ram Laxman
    May 3, 2004
  2. collinm

    strtok problem - strcmp

    collinm, Mar 24, 2005, in forum: C Programming
    Replies:
    4
    Views:
    804
    Mark McIntyre
    Mar 24, 2005
  3. Replies:
    3
    Views:
    501
    Steven Kobes
    Jul 27, 2005
  4. ern

    strtok() problem

    ern, Sep 20, 2005, in forum: C Programming
    Replies:
    12
    Views:
    641
    Default User
    Sep 22, 2005
  5. strtok problem

    , Mar 29, 2006, in forum: C Programming
    Replies:
    6
    Views:
    436
    CBFalconer
    Mar 31, 2006
Loading...

Share This Page