开发者

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

开发者 https://www.devze.com 2023-02-19 22:30 出处:网络
I need help about that script. BOOL Checking(LPCSTR MacID) { char ClientMacs[18] = { \"11:22:33:44:55开发者_JAVA百科:66\",};

I need help about that script.

BOOL Checking(LPCSTR MacID) {
    char ClientMacs[18] = { "11:22:33:44:55开发者_JAVA百科: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.


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


there's and & missing ... non-pointer <-> pointer

BOOL Checking(LPCSTR MacID) {

    const 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;

}

perhaps


I don't think you're quite understanding how strings (or pointers) work in C.

You are trying to compare a single character of your character array to the string being passed in:

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


if(!strcmp(MacID, ClientMacs[x]))
                // ^^^^^^^^^^^ gives the character at index x

Probably you meant -

if(!strcmp(MacID, &ClientMacs[x]))
                //^  Added & symbol

Given the printf statement, I think, there is no need to compare character by character. There is no need of loop. This can be -

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

condensed to -

if(!strcmp(MacID, ClientMacs)) {  // Changed ClientMacs[x] to ClientMacs
    printf(MacID," Successed!");
    return true;
}


ClientMacs needs to be an array of pointers to chars (string pointers), not an array of chars. You might as well use the LPCSTR typedef, because you've also used it for the function parameter.

Try this:

BOOL Checking(LPCSTR MacID) {

    LPCSTR ClientMacs[18] = { "11:22:33:44:55:66", [put the other 9 (or is it 17?) MAC address strings here]};

    for(int x=0; x < 10; x++) {

         if(!strcmp(MacID, ClientMacs[x])) {
            printf(MacID," Successed!");
            return true;
         }
    }
}

Your naming is generally pretty horrible, but I haven't changed that.


Since you've tagged this C++, I'd advise against using strcmp at all, and use std::string instead:

std::set<std::string> ClientMacs;

ClientMacs.insert("11:22:33:44:55:66");
 // presumably insert more MAC addresses here


bool check(std::string const &MacID) {    
    if (ClientMacs.find(MacID) != ClienMacs.end()) {
        std::cout << "Success!";
        return true;
    }
}

I should add, however, that it's not entirely clear what you're trying to accomplish here. My assumption is that you have a list of possible MAC addresses (e.g., of all the computers in your local network) and you're trying to verify that a MAC address you've received (e.g., in an Ethernet packet) matches one of those (e.g., for something on the order of a firewall that will ensure that only packets from known sources are accepted).

0

精彩评论

暂无评论...
验证码 换一张
取 消