Carvallegro
Carvallegro

Reputation: 1341

Get called function name as string

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].

Thanks for helping me :)

Upvotes: 39

Views: 44023

Answers (2)

teppic
teppic

Reputation: 8195

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:

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'

Upvotes: 3

Lundin
Lundin

Reputation: 213809

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.

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:

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

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

Upvotes: 53

Related Questions