Pointers can be declared as pointing to mutable (non-const) data or pointer to constant data.
Pointers can be defined to point to a function.
My coworkers and I were discussing the use of "const" with pointers and the question came up regarding the use of const
with function pointers.
Here are some questions:
typedef void (*Function_Pointer)(void); // Pointer to void function returning void. void function_a(Function_Pointer p_func); // Example 1. void function_b(const Function_Pointer p_func); // Example 2. void function_c(Function_Pointer const p_func); // Example 3. void function_d(const Function_Pointer const p_func); // Example 4.
The above declarations are examples of treating a function pointer like a pointer to an intrinsic type.
A data, variable or memory pointer allows for the above combinations.
So the questions are: can a function pointer have the same combinations and what is meant by a pointer to a const function (such as Example 2)?
In the constant pointers to constants, the data pointed to by the pointer is constant and cannot be changed. The pointer itself is constant and cannot change and point somewhere else.
A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions.
The value of the pointer address is constant that means we cannot change the value of the address that is pointed by the pointer. A constant pointer is declared as follows − Data_Type const* Pointer_Name; For example, int const *p// pointer to const integer.
Pointers can be declared as pointing to mutable (non-const) data or pointer to constant data. Pointers can be defined to point to a function. My coworkers and I were discussing the use of "const" with pointers and the question came up regarding the use of const with function pointers.
In C, there's no such thing as a function being const
or otherwise, so a pointer to a const function is meaningless (shouldn't compile, though I haven't checked with any particular compiler).
Note that although it's different, you can have a const pointer to a function, a pointer to function returning const, etc. Essentially everything but the function itself can be const. Consider a few examples:
// normal pointer to function int (*func)(int); // pointer to const function -- not allowed int (const *func)(int); // const pointer to function. Allowed, must be initialized. int (*const func)(int) = some_func; // Bonus: pointer to function returning pointer to const void const *(*func)(int); // triple bonus: const pointer to function returning pointer to const. void const *(*const func)(int) = func.
As far as passing a pointer to a function as a parameter goes, it's pretty simple. You normally want to just pass a pointer to the correct type. However, a pointer to any type of function can be converted to a pointer to some other type of function, then back to its original type, and retain the original value.
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