Home > Back-end >  using strcmp between string array and 2d array string
using strcmp between string array and 2d array string

Time:12-04

I need to search a word in 2d array an array that I enter but when Im using strcmp function, I have an error "No Matching function for call to 'strcmp'

bool checkIfSameMedicine (char str1[], char str2[][MAXSIZE])
{
    for (int i = 0; i <= 3; i  )
    {
        if (strcmp(str2, str1))
        {
            return true;
        }
        return false;
    }
}

CodePudding user response:

You have to write at least

for (int i = 0; i <= 3; i  )
{
    if (strcmp(str2[i], str1) == 0)
    {
        return true;
    }
}
return false;

Though the code looks not good due to using the magic number 3.

The function should be declared and defined like

bool checkIfSameMedicine( const char str2[][MAXSIZE]), size_t n, const char str1[] )
{
    size_t i = 0;

    while ( i != n && strcmp( str2[i], str1 ) != 0 )   i;

    return n != 0 && i != n;
}
  • Related