Afaik uintptr_t
and intptr_t
can be used to hold any pointer to void
. Hence these types can be used to store pointers to data.
In C99 or later, are there similar signed and unsigned integer types capable of holding pointers to functions?
Afaik uintptr_t
and intptr_t
can be used to hold any pointer to void
. Hence these types can be used to store pointers to data.
In C99 or later, are there similar signed and unsigned integer types capable of holding pointers to functions?
No, there are no such types.
Function pointers may only be reliably cast to other function pointer types (and then, only dereferenced while pointing to the correct function type).
The conversion of function pointers to integers in C is covered by 6.3.2.3/6:
Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
Note that even if the integer type is large enough, casting to integer and back to function pointer is not guaranteed to retrieve the original function pointer.
In C++, the text is in [expr.reinterpret.cast] points 4 and 6. The behaviour is similar, but it explicitly guarantees that if an integer of sufficient size exists, then converting function pointer to integer and back again does retrieve the original function pointer.