在 C/C++ 中获取函数内部函数的地址?

Tür*_*rım 1 c pointers

我试图返回我调用的函数的地址,例如:

void * myfunctionname(some parameters) {
  //some codes
  //more codes.
  return &myfunctionname;
}
Run Code Online (Sandbox Code Playgroud)

如何用void *正确的类型替换以获得正确的签名?

bru*_*uno 6

问题是有正确的返回类型,它是“递归的”,因为它包含自己。

据我所知,拥有“递归”类型的唯一方法是使用structunion,所以

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)

  • @TürkerBerkeYıldırım 你的问题很有趣,我从来没有遇到过这种情况。我认为你必须让你的问题更清楚,问题不在于获取地址,而在于为返回自身的函数提供正确的返回类型 (2认同)