0

I am trying to compile some old code.

Depending on the compiler I get this warning/error when I try to assign a function pointer to a void* pointer:

warning: a value of type "double (*)(double, double)" cannot be assigned to an entity of type "void *"
error: invalid conversion from ‘double (*)(double, double)’ to ‘void*’

I think that the old code used to compile on an old compiler but I understand that the code is illegal and shouldn't compile. I was wondering if there is a void* for functions as well. I've tried this so far:

double sum(double a, double b){return a+b;}

typedef double(*dDd)(double, double);
typedef void(*vFun)(void);
dDd   funPtr  = sum;  // compiles and works
void* vFunPtr = sum;  // does not compile   
vFun  vFun_p  = sum;  // does not compile   

but it fails to compile.

How can I fix this? I would be happy if there were a generalization of void* to functions as well.

jimifiki
  • 5,377
  • 2
  • 34
  • 60
  • 4
    Are you using C or C++? I think it should be allowed in C. C++ requires you to use explicit casts, even when converting to `void*`. – Barmar May 05 '20 at 15:43
  • 2
    IIRC the conversion is _conditionally-supported_ in C++. Some compilers, notably those supporting `dlopen` or `GetProcAddress`, allow it. But it's indeed an explicit conversion if allowed. – MSalters May 05 '20 at 15:45
  • 1
    "Illegal" does not mean "should not compile". It means only that the implementation is required to issue a diagnostic; having done that, the implementation is free to compile the code, with an implementation-specific result. – Pete Becker May 05 '20 at 16:19

3 Answers3

4

What's the void* of function pointers?

In a way: All pointers to functions.

A special thing about pointer to void is that it can point to any object, and any object pointer can be converted to pointer to void and back, which yields the original pointer.

All pointers to functions can be converted to other pointers to function types and be converted back, yielding the original value.


In another way: There is no analogous pointer type for function pointers in the sense that there is no such pointer type into which all function pointer types would implicitly convert to in same manner as object pointers implicitly convert to void*.


How can I fix this?

Convert the pointer explicitly if you need to, using a cast. Use reinterpret_cast in C++.

P.S. Conversion to void* is not guaranteed to work on all systems and is therefore not universally portable. It is "conditionally supported" according to C++ standard. It is guaranteed to work in POSIX conforming systems.

eerorika
  • 232,697
  • 12
  • 197
  • 326
4

There is no designated generic type for function pointers. However, you can use any function pointer type for it. The C standard says any function pointer can be converted to any function pointer type. (Of course, using the converted pointer to call the function is defined only if the type used for the call is compatible with the actual definition of the function.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
2

In C,

A void* can be converted to/from with no trouble to an object pointer.

sum as in void* vFunPtr = sum; does not convert to an object pointer, but a function pointer.

A function pointer and void * may be of different sizes. Casting a function pointer to a void* may lose information. Akin to some_int = some_long;. Info might be lost.

vFun vFun_p = sum; needs a type cast but otherwise works: vFun vFun_p = (vFun) sum;.

void* is not a universal pointer pointer type.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256