Man*_*nav 11 c function-pointers void-pointers
可能重复:
为什么函数指针和数据指针在C/C++中不兼容?
在dlsym的手册页中,提供了以下代码段.
double (*cosine)(double);
handle = dlopen("libm.so", RTLD_LAZY);
/* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
would seem more natural, but the C99 standard leaves
casting from "void *" to a function pointer undefined.
The assignment used below is the POSIX.1-2003 (Technical
Corrigendum 1) workaround; see the Rationale for the
POSIX specification of dlsym(). */
*(void **) (&cosine) = dlsym(handle, "cos");
Run Code Online (Sandbox Code Playgroud)
我查看了相关的spec页面,但仍然无法理解不允许从void指针转换为函数指针的原因.虚假指针应该足够大,以容纳所有类型的指针.如果是这样,为什么不定义这个铸件呢?