调用一个void *作为一个函数没有声明函数指针 [英] Call a void* as a function without declaring a function pointer

查看:543
本文介绍了调用一个void *作为一个函数没有声明函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查,但找不到任何结果(我的术语可能会关闭),所以原谅我,如果这已被问过。

I've searched but couldn't find any results (my terminology may be off) so forgive me if this has been asked before.

我在想,如果有一个简单的方法来调用无效* 如不首先声明一个函数指针,然后分配函数指针的地址C中的函数;

I was wondering if there is an easy way to call a void* as a function in C without first declaring a function pointer and then assigning the function pointer the address;

IE浏览器。假设要调用的函数输入无效(无效)

ie. assuming the function to be called is type void(void)

void *ptr;
ptr = <some address>;
((void*())ptr)(); /* call ptr as function here */

上述code,我得到的错误C2066:强制转换为函数类型是非法的的在VC2008

如果这是可能的,怎么会在语法功能不同,返​​回类型和多个参数?

If this is possible, how would the syntax differ for functions with return types and multiple parameters?

推荐答案

您投应该是:

((void (*)(void)) ptr)();

在一般情况下,这可以通过创建进行简单一个的typedef 为函数指针类型:

In general, this can be made simpler by creating a typedef for the function pointer type:

typedef void (*func_type)(void);
((func_type) ptr)();

这篇关于调用一个void *作为一个函数没有声明函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆