954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to compare stings in linked list

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:

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

tirivamwe
Light Poster
36 posts since Oct 2005
Reputation Points: 11
Solved Threads: 2
 

post the declaration of variable word. It should be

char word[some_value];

or
char *word;
Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

post your code

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

herer is the code of the list:

struct doc_words
       {
        char words[30];
        struct doc_words *next;
       };


and the code of the function:

int getCount(char word)
    {

     int count=0;
     struct doc_words *p = head;
     while (p!=NULL)
           {
            if (strcmp(p->words,word)==0)
               {
                count++;
                p=p->next;
               }
            else
                {
                  p=p->next;
                }
     }
     return(count);
    }
tirivamwe
Light Poster
36 posts since Oct 2005
Reputation Points: 11
Solved Threads: 2
 

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)

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

>>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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 
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.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

>>Ah I know the answer, he didn't.
Your probably right -- he has not written a function that calls getCount() yet.

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

i finally got it right my mistake was here:

int getCount(char word)


it should be:

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

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You