Reputation: 1
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.
Upvotes: 1
Views: 11570
Reputation: 39500
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.
Upvotes: 1
Reputation: 34625
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;
}
Upvotes: 1
Reputation: 490138
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).
Upvotes: 0
Reputation: 76898
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])
Upvotes: 1
Reputation: 8694
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
Upvotes: 3
Reputation: 4845
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
Upvotes: 1