Hi all!
I want to save a strtok into p->word. I tried with this:
...but it doesn't work. Any ideas?Code:strcpy(p->word, (strtok(NULL, " ")));
Thanks in advance for help!
Hi all!
I want to save a strtok into p->word. I tried with this:
...but it doesn't work. Any ideas?Code:strcpy(p->word, (strtok(NULL, " ")));
Thanks in advance for help!
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.
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.
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... }
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.
Stop trying to save yourself half a dozen characters, and write code that's clear and that works: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'.Code:char *t; t = strtok( ...whatever... ) if( t ) strcpy( p->word, t );
Quzah.
Hope is the first step on the road to disappointment.
Thank you for your comment quzah. It solved my problem