Looking to see if anyone knows if its possible to swap C functions...?
void swap2(int(*a)(int), int(*b)(int)) {
int(*temp)(int) = a;
*a = *b;
*b = temp;
// Gives 'Non-object type 'int (int)' is not assignable
}
swap2(&funcA, &funcB);
EDIT
More data here as to intention -- Some answers have been provided below which do work such as creating the function ptr using typedef
, pointing them to the functions and switching those, which lets you invoke the new swapped ptrs successfully.
BUT calling the functions by their original names after swapping shows no change. Essentially I'm looking for a c equivalent of the objc "swizzle".
I'm beginning to think this isn't possible, due to c's complete lack of reflection, and would require actually modifying the binary itself (obviously not feasible). D:
Comments welcome.
I'm pretty sure you need pointers to function pointers to swap pointers, no? This type of swapping function swaps values; you really want to deal in addresses. The example function call wouldn't really work because C doesn't treat functions as first-class variables so you can't actually swap functions directly; you need to use pointers to function addresses, since addresses CAN be swapped:
Your code will give error like "invalid lvalue" at the time of assignment. As I can see in your code you are trying to swap pointers without changing its values so have a look on below solution.
Yes,you can. Think that a function-pointer is just a memory-address,the single requeriment is: where you will keep such address needs to be mutable. Say,
int (*foo)()
not really to wherefoo
points to. May be toprintf()
orfopen()
.Although the subject asks about swapping functions, you actually want to emulate what
swizzle
does. This just means you want to be able to call the same function name but have it do something different.A pointer only solution will not give you that behavior. If that is not important to you, then you should adopt one of the function pointer only solutions provided. If it is important to you, then, you will need to introduce a layer of abstraction. The abstraction could use function pointers under the hood (although there are other solutions).
The API to users of this interface would be:
The implementation of such an interface could look like:
Then, the
swap()
you initially presented in your post could be implemented like this:Here is a program that calls
swap()
:And its output would be:
To automate the adding of abstract functions into the lookup table, you could introduce into the build system an extra step that called a script that would
grep
out theDEFINE_ABSTRACT_FUNC
lines, and generate a new source file that had a function with the calls toinsert()
for each such line.A complete version of the mock-up can be found here.
If you use the function pointers like below, it is yes
Or you use it as this, I think it is no:
Complete compiling working program
Output:
Some people do not try it by themselves, just say it absurd.So I give you a example.