我试图返回我调用的函数的地址,例如:
void * myfunctionname(some parameters) {
//some codes
//more codes.
return &myfunctionname;
}
Run Code Online (Sandbox Code Playgroud)
如何用void *正确的类型替换以获得正确的签名?
问题是有正确的返回类型,它是“递归的”,因为它包含自己。
据我所知,拥有“递归”类型的唯一方法是使用struct或union,所以
struct SFunc
{
struct SFunc (*pf)(int param);
};
struct SFunc myfunctionname(int param)
{
struct SFunc s;
s.pf = myfunctionname;
return s;
}
Run Code Online (Sandbox Code Playgroud)
或者
union UFunc
{
union UFunc (*pf)(int param);
};
union UFunc myfunctionname(int param)
{
union UFunc u;
u.pf = myfunctionname;
return u;
}
Run Code Online (Sandbox Code Playgroud)
编译(两种方式相同):
pi@raspberrypi:/tmp $ gcc -c -Wall -pedantic cc.c
pi@raspberrypi:/tmp $ g++ -c -Wall -pedantic cc.cc
pi@raspberrypi:/tmp $
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |