LoginSignup
0
0

More than 5 years have passed since last update.

Function swizzling in C

Posted at

If a project has below code:

function.h
#include <stdio.h>

extern void function_a();
extern void function_b();
function.c
#include <stdio.h>
#include "function.h"

void function_a() {
    printf("This is function a!\n");
}

void function_b() {
    printf("This is function b!\n");
}
main.c
#include <stdio.h>
#include "function.h"

#define function_b function_a

void function_c() {
    function_a();
    function_b();
}

#undef function_b

int main() {
    function_a();
    function_b();
    function_c();
    return 0;
}

What do you think the result is?

> gcc main.c function.c
> ./a.out
This is function a!
This is function b!
This is function a!
This is function a!

It means you can change the function name before compiling the program, and do things more like:

Call the specific function instead of the origin one to avoid a lot of modification to the code.

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0