35

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

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Carvallegro
  • 1,241
  • 4
  • 16
  • 24
  • 2
    C++ version @ http://stackoverflow.com/q/679021/139010 – Matt Ball Mar 11 '13 at 22:18
  • 1
    There's no standard way of doing this. Which compiler are you using? – John Carter Mar 11 '13 at 22:20
  • 4
    How to get function's name from function's pointer in C http://stackoverflow.com/questions/351134/how-to-get-functions-name-from-functions-pointer-in-c – zzk Mar 11 '13 at 22:20
  • thanks for the quick answers ! @zzk, This topic would've been useful but it's not portable :s I'll think at another way to do this... – Carvallegro Mar 11 '13 at 22:26
  • @MattBall Except that won't work in C, since there is nothing called FUNCTION in C. I believe the correct answer for C would be `__func__`, something your linked post does not cover in any of the answers. – Lundin Mar 11 '13 at 22:41
  • 2
    Vote to reopen since the proposed duplicate does not mention how to properly do this in standard C, with `__func__`. – Lundin Mar 20 '13 at 09:09

2 Answers2

47

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, ...);
  }
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
3

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'
teppic
  • 8,039
  • 2
  • 24
  • 37
  • For performance this is perhaps a better solution as it adds no additional overhead to the function. –  Jun 04 '13 at 12:20