11

可能重复:
为什么 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");

我查看了相关的规范页面,但仍然无法理解不允许从 void 指针转换为函数指针的原因。void 指针是否应该足够大以容纳所有类型的指针。如果是这样,为什么不定义这个铸造?

4

0 回答 0