Search code examples
c++strcmp

error C2664: 'strcmp' : cannot convert parameter 2 from 'char' to 'const char *'


I need help about that script.

BOOL Checking(LPCSTR MacID) {
    char ClientMacs[18] = { "11:22:33:44:55:66",};

    for(int x=0; x < 10; x++) {
        if(!strcmp(MacID, ClientMacs[x])) {
            printf(MacID," Successed!");
            return true;
        }
    }

    return false;
}

I'm getting

error C2664: 'strcmp' : cannot convert parameter 2 from 'char' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

when I try to compile it.


Solution

  • Not

    if(!strcmp(MacID, ClientMacs[x])) {    }
    

    but

    if(!strcmp(MacID, &ClientMacs[x])) { ... }
    

    Arg 2 has to be a char *, but you have it as char. If your arg 2 were plain

      ClientMacs  // compiler understands that this is shorthand for &ClientMacs[0]
    

    it would be fine. But when the index is other than zero, you have to put the ampersand with it.

    -- pete