Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C assign an int to a function pointer

I have an function address stored in an unsigned int, and a function pointer.

Now i want that function_pointer will contain the value of function_address

unsigned int function_address = 0xdeadbeef;
void(*function_pointer)(void) = function_address; // not valid

how to do that in C ?

like image 730
test123423 Avatar asked Jan 25 '26 17:01

test123423


2 Answers

Perform an explicit cast.

void(*function_pointer)(void) = (void (*)(void))function_address;
like image 164
R Sahu Avatar answered Jan 28 '26 08:01

R Sahu


Try this

typedef void(*function_pointer_type)(void)
function_pointer_type *function_pointer = (function_pointer_type *) address;
like image 26
Iharob Al Asimi Avatar answered Jan 28 '26 07:01

Iharob Al Asimi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!