将被调用的函数名称作为字符串

时间:2022-04-02 16:45:04

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 :)

谢谢你帮助我:)

2 个解决方案

#1


21  

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, ...);
  }
}

#2


1  

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'

#1


21  

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, ...);
  }
}

#2


1  

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'