错误 C2664:“strcmp” : 无法从“char”转换参数 2到“const char *”
我需要有关该脚本的帮助。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不是
,但
Arg 2 必须是 char *,但你将它作为 char 。如果你的 arg 2 是普通的
那就没问题了。但是当索引不为零时,您必须在其上加上“&”号。
——皮特
Not
but
Arg 2 has to be a char *, but you have it as char. If your arg 2 were plain
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
perhaps
我认为您不太了解字符串(或指针)在 C 中的工作原理。
您试图将字符数组中的单个字符与传入的字符串进行比较:
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:
可能您的意思是 -
鉴于
printf
语句,我认为无需逐个字符进行比较。不需要循环。这可以 -浓缩为 -
Probably you meant -
Given the
printf
statement, I think, there is no need to compare character by character. There is no need of loop. This can be -condensed to -
ClientMacs 需要是一个指向字符的指针数组(字符串指针),而不是一个字符数组。您也可以使用 LPCSTR typedef,因为您也将它用于函数参数。
试试这个:
你的命名通常很糟糕,但我没有改变它。
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:
Your naming is generally pretty horrible, but I haven't changed that.
既然你已经标记了这个 C++,我建议不要使用
strcmp
,而是使用std::string
:但是,我应该补充一点,它并不完全是明确你想在这里完成什么。我的假设是,您有一个可能的 MAC 地址列表(例如,您本地网络中的所有计算机的 MAC 地址),并且您正在尝试验证您收到的 MAC 地址(例如,在以太网数据包中)是否与以下之一匹配:这些(例如,防火墙之类的东西将确保只接受来自已知来源的数据包)。
Since you've tagged this C++, I'd advise against using
strcmp
at all, and usestd::string
instead: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).