Reputation: 12683
Can I test the type of a void pointer? I want to solve this problem:
void log(void *test) {
if (is a struct) {
NSLog(test);
} else {
printf("%s\n", test);
}
}
log(@"This send a struct (NSString)");
log("This send normal string");
There are a easy way to test it?
Upvotes: 0
Views: 1183
Reputation: 180917
There's really no way to do that kind of detection in a non implementation specific way since when you cast the NSString to a void* you throw away all typing information and convert it to an "untyped pointer to memory".
I'm not sure why you want this kind of functionality, but if it's between two objects, you should use "id" instead of void* since that preserves all type information.
In this specific example, it would probably make a lot more sense to just make "log" an overloaded function and thereby know the type of the parameter compile time.
Upvotes: 0
Reputation: 7164
No you cannot test this. A void* is just a memory address where anything (or even nothing) could be stored. It's meaning is entirely implementation dependent and only the original programmer (or the documentation) knows.
Upvotes: 6