简体   繁体   中英

How can I modify a function by C pointer?Can it be done?

First, define two functions. Then, assign the address represented by the function name to two ordinary pointers. Finally, copy the value pointed to by one pointer to the memory pointed to by another pointer. Output the same result by calling different functions? How can i achieve it? Or can it be achieved?

#include <stdio.h>
#include <stdlib.h>

void f(int *x)
{
    int j=7;
    *x=j;
    printf("%d-",*x);

} 

int main()
{
    int i=66 ;
    int *x;
    x=&i;
    f(x);
    printf("%d",*x);

    return 0;

}

C does not support modifying functions at run-time. Copying the bytes of one function to another function is not likely to work. In common C implementations, the memory of functions is marked read-only, and modifying it requires special steps to change that. Even if you can write to the memory, instructions may be different depending on where they are located in memory—they may use absolute addresses or offsets from special reference points—so the copied instructions will not work in their new locations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM