I saw a piece of code like this:
#include <windows.h>
static VOID (WINAPI * pFun) (DWORD seconds) = NULL;
void foo(PVOID * par){
return;
}
int main(){
foo(&(PVOID&) pFun); // type-cast 1
foo((PVOID *) (&pFun)); // type-cast 2
return 0;
}
I can understand the type-cast 2, but how does type-cast 1 work?
First of all, you’re using C++.
In
windows.hVOIDandPVOIDare defined asvoidandvoid*respectively.Let’s look at a basic program:
In the first case, we’re taking the address of
p, which isint**, and passing it tofoo()by casting it tovoid**.In the second case, we’re first casting
ptovoid*&and then taking its address, which would bevoid**, and passing that tofoo(). The reason it’svoid*&and notvoid*is that you can use the unary&operator (address of) only on an lvalue.