C语言 获取被调用的函数名称作为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15349761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:40:08  来源:igfitidea点击:

Get called function name as string

c

提问by Carvallegro

I'd like to display the name of a function I'm calling. Here is my code

我想显示我正在调用的函数的名称。这是我的代码

void (*tabFtPtr [nbExo])(); // Array of function pointers
int i;
for (i = 0; i < nbExo; ++i)
{
    printf ("%d - %s", i, __function__);
}

I used __function__as an exemple because it's pretty close from what I'd like but I want to display the name of the function pointed by tabFtPtr [nbExo].

我用作__function__示例是因为它与我想要的非常接近,但我想显示tabFtPtr [nbExo].

Thanks for helping me :)

谢谢你帮助我:)

回答by Lundin

You need a C compiler that follows the C99 standard or later. There is a pre-defined identifier called __func__which does what you are asking for.

您需要遵循 C99 标准或更高版本的 C 编译器。有一个名为的预定义标识符可以满足__func__您的要求。

void func (void)
{
  printf("%s", __func__);
}

Edit:

编辑:

As a curious reference, the C standard 6.4.2.2 dictates that the above is exactly the same as if you would have explicitly written:

作为一个奇怪的参考,C 标准 6.4.2.2 规定上述内容与您明确编写的内容完全相同:

void func (void)
{
  static const char f [] = "func"; // where func is the function's name
  printf("%s", f);
}

Edit 2:

编辑2:

So for getting the name through a function pointer, you could construct something like this:

因此,要通过函数指针获取名称,您可以构造如下内容:

const char* func (bool whoami, ...)
{
  const char* result;

  if(whoami)
  {
    result = __func__;
  }
  else
  {
    do_work();
    result = NULL;
  }

  return result;
}

int main()
{
  typedef const char*(*func_t)(bool x, ...); 
  func_t function [N] = ...; // array of func pointers

  for(int i=0; i<N; i++)
  {
    printf("%s", function[i](true, ...);
  }
}

回答by teppic

I'm not sure this is what you want, but you could do something like this. Declare a structure to hold a function name and address, and an array of functions at file scope:

我不确定这是你想要的,但你可以做这样的事情。声明一个结构来保存函数名称和地址,以及文件范围内的函数数组:

#define FNUM 3

struct fnc {
    void *addr;
    char name[32];
};

void (*f[FNUM])();
struct fnc fnames[FNUM];

Initialise these in your code manually by function name, e.g.

通过函数名称在您的代码中手动初始化这些,例如

    fnames[0] = (struct fnc){foo1, "foo1"}; // function address + its name
    fnames[1] = (struct fnc){foo2, "foo2"};
    fnames[2] = (struct fnc){foo3, "foo3"};

Make a function to search the array, e.g.

制作一个函数来搜索数组,例如

char *getfname(void *p)
{
        for (int i = 0; i < FNUM; i++) {
                if (fnames[i].addr == p)
                        return fnames[i].name;
        }
        return NULL;
}

I ran a quick test of this. I initialised the array in main, and called foo1(). Here's my function, and the output:

我对此进行了快速测试。我在 中初始化了数组main,然后调用了foo1(). 这是我的函数和输出:

void foo1(void)
{
    printf("The pointer of the current function is %p\n", getfnp(__func__));
    printf("The name of this function is %s\n", getfname(getfnp(__func__)));
    printf("The name of the function at pointer f[2] (%p) is '%s'\n", f[2],
        getfname(f[2]));    
}

The pointer of the current function is 0x400715
The name of this function is foo1
The name of the function at pointer f[2] (0x40078c) is 'foo3'

Or, more generally:

或者,更一般地说:

void foo2(void)
{
    for (int i = 0; i < FNUM; i++) {
        printf("Function f[%d] is called '%s'\n", i, getfname(f[i]));
    }
}

Function f[0] is called 'foo1'
Function f[1] is called 'foo2'
Function f[2] is called 'foo3'