On Feb 18, 12:12 pm, "MisterE" <Mist...@nimga.comwrote:
Is it possible to create a pointer to a function
Sure, T (*ptr)(T); declares ptr as a function pointer that takes T and
returns T.
and then get its size (the actual size the function takes in machine code),
Nope, that cannot be done. There is not even machine code in a
function pointer, and a function pointer does not have to point to
actual memory in the implementation.
such that you can copy the function to another memory location. You could then modify it (I know it
You can do that
int (*ptr)(int) = putchar;
T (*tmp)(T) = (T (*)(T))ptr; /* any type T is, this is guaranteed to
work, cast is not needed */
ptr = getchar;
ptr();
ptr = (int (*)(int))tmp; /* this is guaranteed to work too, cast not
needed */
ptr('\n');
What my code demonstrates here is that there is no 'void *' for
function pointers because you can store any function pointer to any
other function pointer and back.
would be modifing the machine code) and then call the modified function via
a function pointer?
ISO C does not define 'machine code'.
Why do you ask here? try it!
It doesn't seem to me you care about ISO C or portability, rather than
getting that 'hack' work.