0

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

3
Contributors
8
Replies
9
Views
11 Years
Discussion Span
Last Post by tirivamwe
0

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);
    }
0

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)

0

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

0

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.

0

i finally got it right my mistake was here:

int getCount(char word)

it should be:

int getCount(char word[30])
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.