ISO C空*和功能指针 [英] ISO C Void * and Function Pointers

查看:98
本文介绍了ISO C空*和功能指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了一些教程并阅读了有关函数指针的知识后,我了解到在ISO C中显然没有为函数指针分配空指针,但是有什么方法可以解决我在编译期间收到的警告(例如,更好的编码方式)它)还是我应该忽略它?

While following some tutorials and reading about function pointers I learned that evidently assigning a void pointer to a function pointer in ISO C is undefined, is there any way to resolve the warning I receive during compile time (e.g. a better way of coding it) or should I just ignore it?

警告:

ISO C forbids assignment between function pointer and 'void *' [-pedantic]

示例代码:

void *(*funcPtr)();
funcPtr = GetPointer();

GetPointer是一个返回空指针E.G.的函数.

GetPointer is a function that returns a void pointer E.G.

void *GetPointer();

推荐答案

tlpi-book 我发现这个技巧非常有趣:

In tlpi-book I found this trick very interesting:

#include <dlfcn.h>

int
main(int argc, char *argv[])
{
    ...
    void (*funcp)(void);        /* Pointer to function with no arguments */
    ...
    *(void **) (&funcp) = dlsym(libHandle, argv[2]);
}

这篇关于ISO C空*和功能指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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