0

I have a function pointer defined like below:

typedef void (*FPT)(void);
FPT Fp;

The pointer variable "Fp" is located at address 0x1234 I have my function defined like below:

void myfunc (void)
{
    return;
}

I do not have access to the symbol name "Fp" but I know its address (0x1234). Now how do I assign the address of myfunc() to "Fp" ??

1 Answer 1

1

This should do what you want.

FPT *fpt_pointer = (FPT *)0x1234;

*fpt_pointer = myfunc;

But note that it's very bad practice to have hard coded addresses like this. I don't know whether you really intend to do that (don't!) or whether you are just describing it that way to simplify the question. It is very likely to break the next time you run the program (even without a re-compile due to Address Space Layout Randomisation).

Sign up to request clarification or add additional context in comments.

3 Comments

Not to mention 0x1234 being a fairly implausible address for a function.
@JonathanLeffler It was bogus, ofc. *(FPT *)offset = myfunc; is fine too.
This is exactly what I am trying, just wanted to see if there are other ways. And yes, it is just a random address I typed in. I definitely dont intent to put a hard coded address. It is just easier to explain the question here. Thanks guys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.