错误 C2664:“strcmp” : 无法从“char”转换参数 2到“const char *”

发布于 2024-10-27 05:57:15 字数 485 浏览 1 评论 0原文

我需要有关该脚本的帮助。

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

我得到了

错误 C2664:“strcmp”:无法转换 参数 2 从 'char' 到 'const char *' 从整型到指针类型的转换需要 reinterpret_cast,C 风格强制转换或 函数式转换

当我尝试编译它时

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

水晶透心 2024-11-03 05:57:15

不是

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

,但

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

Arg 2 必须是 char *,但你将它作为 char 。如果你的 arg 2 是普通的

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

那就没问题了。但是当索引不为零时,您必须在其上加上“&”号。

——皮特

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

万水千山粽是情ミ 2024-11-03 05:57:15

有 和 &缺少...非指针<-> 指针

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;

}

也许是

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

只是在用心讲痛 2024-11-03 05:57:15

我认为您不太了解字符串(或指针)在 C 中的工作原理。

您试图将字符数组中的单个字符与传入的字符串进行比较:

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

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])
月亮邮递员 2024-11-03 05:57:15
if(!strcmp(MacID, ClientMacs[x]))
                // ^^^^^^^^^^^ gives the character at index x

可能您的意思是 -

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

鉴于 printf 语句,我认为无需逐个字符进行比较。不需要循环。这可以 -

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

浓缩为 -

if(!strcmp(MacID, ClientMacs)) {  // Changed ClientMacs[x] to ClientMacs
    printf(MacID," Successed!");
    return true;
}
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;
}
甜宝宝 2024-11-03 05:57:15

ClientMacs 需要是一个指向字符的指针数组(字符串指针),而不是一个字符数组。您也可以使用 LPCSTR typedef,因为您也将它用于函数参数。

试试这个:

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

你的命名通常很糟糕,但我没有改变它。

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.

又怨 2024-11-03 05:57:15

既然你已经标记了这个 C++,我建议不要使用 strcmp ,而是使用 std::string

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

但是,我应该补充一点,它并不完全是明确你想在这里完成什么。我的假设是,您有一个可能的 MAC 地址列表(例如,您本地网络中的所有计算机的 MAC 地址),并且您正在尝试验证您收到的 MAC 地址(例如,在以太网数据包中)是否与以下之一匹配:这些(例如,防火墙之类的东西将确保只接受来自已知来源的数据包)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文