can void* be used to store function pointers?

c, c++, function-pointers, void-pointers

Solution

No it may not.

According to the C Standard (6.3.2.3 Pointers)

1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

As for function pointers then

8 A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.

In the C++ Standard there is more detailed definition of pointers (3.9.2 Compound types)

3 The type of a pointer to void or a pointer to an object type is called an object pointer type....The type of a pointer that can designate a function is called a function pointer type.

And

4 A pointer to cv-qualified (3.9.3) or cv-unqualified void can be used to point to objects of unknown type. Such a pointer shall be able to hold any object pointer. An object of type cv void* shall have the same representation and alignment requirements as cv char*.

Problem

void* is defined in such a way that it could point any thing. So can it be used to point a function (int send())? ``` int send(); void* p = send; ``` Is it possible? When i use like this it is not showing me errors why? If not, Is there any way to store all pointers in a single variable?

Original source

Related problems