A function is where? In memory, right? And all memory is addressable, right? So the question is why can a function be addressed? In fact a function is always addressed, even if you don’t see the pointer to it (very common for most languages to hide pointers), yet when it calls a function it is actually diverting the execution of the program to the given address according to the name of that function. The only way to access a function is by its pointer. What you are seeing there in this code is only one indirect extra created to personalize the call.
If you think a function has no address it’s because you think it’s not in the memory and if it’s not there, where is it? Or do you think there’s some part of the memory that’s not addressable, so the question would be why do you think that? Don’t make exceptions to the concept where you don’t need to.
Function pointers are no different than "normal" because they are normal, they only point to a function and if it is to use so it takes a syntax that indicates either a pointer to a function, which indicates to the compiler that that variable can be called and then it allows that call when it is made because it knows that it is not a simple given.
Pointer is the mechanism and it doesn’t change. The type of information the pointer points to changes, then the typing indicates something different and this is a case that requires an exception in the syntax.
If you want to know why you don’t use the operator &
to get the function address is precisely because it is already an address (is the same question as that question), then not that do any operation to pick up the address, unlike a given you should create a pointer to be able to use if it is not yet an address.
Although it works, you don’t use this type of code in C++, you have better, more robust and more readable ways of doing this in C++, so you’re showing in the code how you do it in C, even if you do it with C++, but that’s another question.
A function is stored IN ONE memory address. What changes to a variable, for example, is the form that is accessed/used. In the case of a function, in asm x86 msvc is used the instruction
CALL
to call her (which has a small differences ofJMP
). A variable can be in the address 0x00004 the function in 0x00008, will change the way they are used through specific functions of Assembly,– Kevin Kouketsu