I have just read an interesting question here that makes me wonder about two more things:
void *
or does it hold richer information (like return type, number of arguments and arguments types?)Function Pointers point to code like normal pointers. In Functions Pointers, function's name can be used to get function's address. A function can also be passed as an arguments and can be returned from a function.
Function Pointers are pointers, i.e. variables, which point to an address of a function. The running programs get a certain space in the main memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is also an address.
Function pointers can be useful when you want to create callback mechanism, and need to pass address of a function to another function. They can also be useful when you want to store an array of functions, to call dynamically for example.
A function to pointer is one which returns a pointer. A pointer to function points to a function.
Why should anyone compare function pointers? Here's one example:
#include <stdbool.h> /* * Register a function to be executed on event. A function may only be registered once. * Input: * arg - function pointer * Returns: * true on successful registration, false if the function is already registered. */ bool register_function_for_event(void (*arg)(void)); /* * Un-register a function previously registered for execution on event. * Input: * arg - function pointer * Returns: * true on successful un-registration, false if the function was not registered. */ bool unregister_function_for_event(void (*arg)(void));
The body of register_function_for_event
only sees arg
. It doesn't see any function name. It must compare function pointers to report someone is registering the same function twice.
And if you want to support something like unregister_function_for_event
to complement the above, the only information you have is the function address. So you again would need to pass it in, and compare against it, to allow removal.
As for the richer information, yes. When the function type contains a prototype, it's part of the static type information. Mind you that in C a function pointer can be declared without a prototype, but that is an obsolescent feature.
Why would someone compare pointers? Consider the following scenario -
You have an array of function pointers, say it is a call back chain and you need to call each one of them. The list is terminated with a NULL
(or sentinel) function pointer. You need to compare if you have reached the end of the list by comparing with this sentinel pointer. Also, this case justifies previous OPs concern that different functions should have different pointers even if they are similar.
Does the compiler see them differently? Yes. The type information includes all the information about the arguments and the return type.
For example, the following code will/should be rejected by the compiler -
void foo(int a); void (*bar)(long) = foo; // Without an explicit cast
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With