-1

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?

Fantety
  • 25
  • 1
  • 3
  • 1
    Have you tried to do the steps that are described here? At which step are you stuck? What is the problem? – mkrieger1 May 09 '21 at 13:48
  • 2
    Please check these answer. It already given on stackoverflow [ModifyPointer](https://stackoverflow.com/questions/35609109/modify-pointer-value-in-a-function/35609326) [ModifyPointer](https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c) – Muhammad May 09 '21 at 13:50
  • Why is the sentence "Output the same result by calling different functions" written like a question? – mkrieger1 May 09 '21 at 13:51
  • Actually this is not a problem, it’s just that I’m curious. It seems that someone has already explained. – Fantety May 09 '21 at 23:31

2 Answers2

0
#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;

}
Muhammad
  • 19
  • 2
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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312