Given that all the primitive data types and objects have memory allocated, it is intuitively easy to imagine the pointers to these types.
But where exactly do function p
Function pointers point to the address of the function in memory.
Based on the way function pointers are usually assigned, I would be surprised if you had them pointing to a data location. They are not typically cast and so unlikely to point anywhere other than to a valid function. If you are casting them a lot, then this could be a problem. More likely though is that the data you are passing to the function is wrong.
A function pointer contains the address of a function -- whatever that means for a given system. You can use it to call the function indirectly, you can assign and compare function pointer values. You can also convert a function pointer to another pointer-to-function type, and so forth.
The most straightforward way for a compiler to implement a function pointer is as the memory address of the function, and most compilers do it that way, but the language standard doesn't specify that. Some systems (I think IBM's AS/400 is an example) store additional information in function pointers. A compiler could implement function pointers as integer indices into a table of descriptors, as long as the required semantics are implemented properly.
It's not possible to dereference a function pointer. A function call requires a pointer, not a function name, as its prefix expression; if you use a function name, that name is implicitly converted to a pointer (as far as the language is concerned; the generated machine code might be different). So there's no portable way for a program to detect whether function pointers actually contain memory addresses or not.
Non-portably, given a function func
, you can do something like:
printf("Address of func is %p\n", (void*)func);
and you'll probably see something that looks like a human-readable representation of a memory address. But strictly speaking, the behavior of the conversion of the function pointer to void*
is not defined by the language, and it might not work on some systems.
You might even be able to convert a function pointer to unsigned char*
and examine the function's machine code, but goes far beyond anything defined by the C standard.
It's best to treat function pointers as opaque values that refer to particular functions in some unspecified manner.
Well I'm not sure but considering that functions are instructions(ADD, SUB, JMP) and that each of them have hexadecimal values, I believe that you would not be altering the function but only the JMP instruction()...
Function pointer also point into memory, the only difference is that there is executable code at that memory location instead of data.
On many platforms if you try to execute data (e.g. regular memory) you'll crash or cause an exception. This is known as Data Execution Prevention - a security measure to prevent applications inadvertently running dodgy code that may be placed there by malware.
It's a code pointer. It points to the function's address. It is essentially as you described. And yes, if you have pointers that don't point to what you expect, you will have problems.