static_cast<const char *>
should work fine!
The following code snippet demonstrates it.
#include <iostream>
bool compare(const void *p, const void *q){
const char * casted_p = static_cast<const char*>(p);
const char * casted_q = static_cast<const char*>(q);
return *casted_p == *casted_q;
}
int main(int argc, char * argv[]) {
char c1 = '6';
char c2 = '6';
const char *p1, *p2;
p1 = &c1;
p2 = &c2;
if(compare(p1,p2)){
std::cout << "equal" << std::endl;
} else {
std::cout << "diff" << std::endl;
}
return 0;
}
This will print equal
.
If one character is changed to something else, e.g 7, it will print diff
.
I am not sure what would be the purpose of such a function, but it can be done!
Note: If what you are up to is type erasure, consider templates! It is safer and more modern approach. Using void *
for such a purpose is still C-style.