strcpy and strtok

This is a discussion on strcpy and strtok within the C Programming forums, part of the General Programming Boards category; Hi all! I want to save a strtok into p->word. I tried with this: Code: strcpy(p->word, (strtok(NULL, " "))); ...but ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    61

    strcpy and strtok

    Hi all!

    I want to save a strtok into p->word. I tried with this:

    Code:
    strcpy(p->word, (strtok(NULL, " ")));
    ...but it doesn't work. Any ideas?

    Thanks in advance for help!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by sababa.sababa View Post

    ...but it doesn't work. Any ideas?
    You win the weekly prize for not providing sufficient information.
    Last edited by Dino; 12-10-2009 at 01:08 PM. Reason: typo - I win th eprize for most typos. lol
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Since that doesn't work, you might as well rewrite it so that you can check strtok's return value. If it returns NULL earlier than you expected, you did something wrong. For example, when you call strtok(NULL, " "); NULL only works as a first argument after calling strtok with a different argument than NULL. This includes pointers that point to NULL.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It'll also only work if you actually have a space in the string you've already strtok-ed. If it doesn't, you're still going to get NULL back from it.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    61

    Thumbs up

    Thanks whiteflags!

    The problem seems to be that the first strtok isn't in the same function as the other, and I don't know how to pass p->word into the second function. Here is a sketch of the code that doesn't work:

    Code:
    some headers...
    
    struct node
    {
    	char *word;
    	struct node *next;
    };
    
    char function(char* p->word)
    {
    ...some code
    	strcpy(p->word, (strtok(NULL, " ")));;
    ...some code.
    }
    
    int main(void)
    {
    	p = malloc(sizeof (struct *node));
    	p->word = malloc(40);
    
    	...some code...
    	strcpy(p->word, (strtok(buf, " ")));;
    	function(p->word);
    	...some code...
    }

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    That doesn't even compile. Type that in your IDE and give it a test compile; see what you can fix with the error messages. Then follow the rest of the advice in this thread. I still think that you need to rewrite it so that you can check strtok's return value.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop trying to save yourself half a dozen characters, and write code that's clear and that works:
    Code:
    char *t;
    t = strtok( ...whatever... )
    if( t )
        strcpy( p->word, t );
    If you can't write elegant code right off, at least make sure it works right. Then worry about streamlining it and making it 'purdy'.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thank you for your comment quzah. It solved my problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok() and strcpy
    By grimlark in forum C Programming
    Replies: 7
    Last Post: 05-27-2009, 03:11 AM
  2. Strtok Problem.
    By sergioms in forum C Programming
    Replies: 8
    Last Post: 11-23-2008, 11:03 AM
  3. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 05:48 PM
  4. What's up with this strcpy?
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 05:24 PM
  5. help with strtok
    By requiem in forum C Programming
    Replies: 5
    Last Post: 04-29-2003, 04:10 PM

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