可能重复:
Why are function pointers and data pointers incompatible in 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");
我查找了相关的spec page,但仍然无法理解不允许从void指针转换为函数指针的原因。 Aren的void指针应该足够大以容纳所有类型的指针。如果是这样,为什么要保留这个铸件未定义?