how to compare stings in linked list

Reply

Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

how to compare stings in linked list

 
0
  #1
Oct 23rd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,590
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how to compare stings in linked list

 
0
  #2
Oct 23rd, 2006
post the declaration of variable word. It should be
  1. char word[some_value];
  2.  
  3. or
  4. char *word;
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: how to compare stings in linked list

 
0
  #3
Oct 23rd, 2006
post your code
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

Re: how to compare stings in linked list

 
0
  #4
Oct 23rd, 2006
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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: how to compare stings in linked list

 
0
  #5
Oct 23rd, 2006
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 8:50 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,590
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how to compare stings in linked list

 
0
  #6
Oct 23rd, 2006
>>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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: how to compare stings in linked list

 
0
  #7
Oct 23rd, 2006
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 9:19 am.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,590
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how to compare stings in linked list

 
0
  #8
Oct 23rd, 2006
>>Ah I know the answer, he didn't.
Your probably right -- he has not written a function that calls getCount() yet.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

Re: how to compare stings in linked list

 
0
  #9
Oct 23rd, 2006
i finally got it right my mistake was here:

  1. int getCount(char word)

it should be:

  1. int getCount(char word[30])
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2178 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC