Function pointers are variables, just like ints and doubles. The address of a function is something different. It is the location of the beginning of the function in the .text
section of the binary. You can assign the address of a function to a function pointer of the same type however the .text
section is read only and therefore you can't modify it. Writing to the address of a function would attempt to overwrite the code at the beginning of the function and is therefore not allowed.
Note:
If you want to change, at runtime, where function calls end up you can create something called a vritual dispatch table, or vtable. This is a structure containing function pointers and is used in languages such as c++ for polymorphism.
e.g.:
struct VTable {
int (*foo)(void);
int (*bar)(int);
} vTbl;
At runtime you can change the values of vTbl.foo
and vTbl.bar
to point to different functions and any calls made to vTbl.foo()
or .bar
will be directed to the new functions.