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

Dani AI

Generated

The warning came from passing a single char where a pointer to a null-terminated string was required. and were right to flag the prototype issue, and 's change to use a buffer fixed the immediate compile warning. For correctness and safety, prefer a function that accepts a const char * and always verify null-termination and NULL pointers.

A compact, safe pattern (names chosen differently from the thread) — traverse the list, guard the parameter, and use strcmp on well-formed strings:

typedef struct WordNode {
    char text[64];
    struct WordNode *next;
} WordNode;

/* head is the start of the list */
int count_matches(const char *needle)
{
    int matches = 0;
    WordNode *cur = head;
    if (!needle) return 0;
    while (cur) {
        if (strcmp(cur->text, needle) == 0)
            ++matches;
        cur = cur->next;
    }
    return matches;
}

Practical safety notes:

  • Always ensure stored fixed-size buffers are null-terminated when you fill them. Prefer snprintf(dest, sizeof dest, "%s", src) or strlcpy (where available) over strncpy to avoid unterminated strings.
  • If you want case-insensitive comparisons, use strcasecmp (POSIX) or _stricmp on Windows. For partial matches, use strncmp.
  • For variable-length words, store char * and strdup on insert; free on removal to avoid truncation and preserve full words.
  • Enable compiler warnings (-Wall -Wextra) and put the function prototype before its use so the compiler detects type mismatches early.

These practices avoid the subtle runtime bugs that follow a silent mismatch between a single char and a string pointer.

Recommended Answers

All 8 Replies

post the declaration of variable word. It should be

char word[some_value];

or
char *word;

post your code

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

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)

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

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.

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

i finally got it right my mistake was here:

int getCount(char word)

it should be:

int getCount(char word[30])
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.