2015-09-28 37 views
3

我有一个存储在unsigned int中的函数地址和一个函数指针。C给函数指针分配一个int

现在我想的是function_pointer将包含function_address

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

怎么做,在C值?

+1

PSA:不要使用'unsigned int'来存储地址。在某些平台上(比如x86_64 Linux),int值小于指针。为此,使用'uintptr_t'类型。 – fuz

回答

7

进行显式类型转换。

void(*function_pointer)(void) = (void (*)(void))function_address; 
+0

ahh nice thx very – test123423

+0

@ test123423,欢迎您。 –

1

试试这个

typedef void(*function_pointer_type)(void) 
function_pointer_type *function_pointer = (function_pointer_type *) address;