943,882 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2813
  • C RSS
Oct 23rd, 2006
0

how to compare stings in linked list

Expand Post »
i have this line of code and want to compare the word from the linked list with the one supplied by user.

the code is:
  1. if (strcmp(p->word,word)==0)

and i am having this error:

passing `char' to argument 2 of `strcmp(const char *, const char *)' lacks a cast
Similar Threads
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Oct 23rd, 2006
0

Re: how to compare stings in linked list

post the declaration of variable word. It should be
  1. char word[some_value];
  2.  
  3. or
  4. char *word;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,952 posts
since Aug 2005
Oct 23rd, 2006
0

Re: how to compare stings in linked list

post your code
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 23rd, 2006
0

Re: how to compare stings in linked list

herer is the code of the list:

  1. struct doc_words
  2. {
  3. char words[30];
  4. struct doc_words *next;
  5. };

and the code of the function:


  1. int getCount(char word)
  2. {
  3.  
  4. int count=0;
  5. struct doc_words *p = head;
  6. while (p!=NULL)
  7. {
  8. if (strcmp(p->words,word)==0)
  9. {
  10. count++;
  11. p=p->next;
  12. }
  13. else
  14. {
  15. p=p->next;
  16. }
  17. }
  18. return(count);
  19. }
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Oct 23rd, 2006
0

Re: how to compare stings in linked list

word is a single char and strcmp takes char *. So you need to pas strcmp(p->words,&word) as second parameter. Are you shore:
int getCount(char word) is right. Becouse there are more sense if it was int getCount(char * word)
Last edited by andor; Oct 23rd, 2006 at 9:50 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 23rd, 2006
0

Re: how to compare stings in linked list

>>int getCount(char word)

that should be like this
int getCount(char*  word)

>>strcmp(p->words,&word)
that won't work either. strcmp() expects a null-terminated string. Just passing the address of word does not make it null-terminated.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,952 posts
since Aug 2005
Oct 23rd, 2006
0

Re: how to compare stings in linked list

Quote ...
that won't work either. strcmp() expects a null-terminated string. Just passing the address of word does not make it null-terminated.
I agree, my mistake. I didn't think just saw that strcmp accepts pointer to char. But how did he pass the whole string inside one char?!?
Ah I know the answer, he didn't.
Last edited by andor; Oct 23rd, 2006 at 10:19 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 23rd, 2006
0

Re: how to compare stings in linked list

>>Ah I know the answer, he didn't.
Your probably right -- he has not written a function that calls getCount() yet.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,952 posts
since Aug 2005
Oct 23rd, 2006
0

Re: how to compare stings in linked list

i finally got it right my mistake was here:

  1. int getCount(char word)

it should be:

  1. int getCount(char word[30])
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: am i using fgets correctly? (Cprog)
Next Thread in C Forum Timeline: GTK code uses pointer for structs and not normal instances why?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC