-4

So I'm passing a char Array(En) that consists a few words and I'm trying to sort alphabetically. Unfortunately, my compiler explodes with " int strcmp(const char *,const char *)' : cannot convert argument 1 from 'char' to 'const char *" and I'm kinda stuck!

void TDihotTable::Set(char *En){
    int i, j;
    bool sorted = false;
    char* pTemp = NULL;
    while (!sorted)
    {
        sorted = true;
        for (size_t i = 0; i < 6 - 1; ++i)
        {
            if (!strcmp(En[i], En[i + 1]) > 0)
            {
                sorted = false;
                pTemp = En[i];
                En[i] = En[i + 1];
                En[i + 1] = pTemp;
            }
        }
    }
}

5 Answers5

0

strcmp() expects it's arguments to be of const char *, but in your code, En[i] and En[i+1] are of type char.

  • if you want to comapre chars, use == operator.
  • if you want to compare strings, pass the address using &En[i] and &En[i+1]

That said,

 if (!strcmp(En[i], En[i + 1]) > 0)

is wrong. Please verify your logic.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Use En[i] != En[i + 1] to compare characters. strcmp takes char * arguments but En[i] and En[i + 1] are char.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

strcmp() requires strings as arguments for comparison, not chars. So, instead of this line:

 if (!strcmp(En[i], En[i + 1]) > 0)

You can directly compare the two chars as if(En[i]==En[i+1]) or if(En[i]!=En[i+1]) as per your requirements.

Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
0

Array of words is supposed to be a two dimensional array of characters: char **En. The comparison should work then.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
0

Your function should look more like:

void TDihotTable::Set(char **En){
                           ^^

That would be a array of pointers to string, which you can strcmp like you done in

       if (!strcmp(En[i], En[i + 1]) > 0)
           ^

which is als so buggy at the mark. use

       if (strcmp(En[i], En[i + 1]) > 0)

at the moment you try to compare single characters.

Martin Schlott
  • 4,369
  • 3
  • 25
  • 49