40

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

6
  • 2
    C++ version @ stackoverflow.com/q/679021/139010 Commented Mar 11, 2013 at 22:18
  • 1
    There's no standard way of doing this. Which compiler are you using? Commented Mar 11, 2013 at 22:20
  • 4
    How to get function's name from function's pointer in C stackoverflow.com/questions/351134/… Commented Mar 11, 2013 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... Commented Mar 11, 2013 at 22:26
  • 2
    Vote to reopen since the proposed duplicate does not mention how to properly do this in standard C, with __func__. Commented Mar 20, 2013 at 9:09

2 Answers 2

54

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, ...);
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

You can't get at that string from a function pointer, though, can you?
@CarlNorum Simply call the function. The function will have to integrate the func identifier somehow.
I don't really see how this can integrate with the OP's question, that's all. He doesn't appear to want to call the functions, just grab their names at runtime.
@CarlNorum Ill add an example.
Sorry for the late answer. It working the way I wanted now, perfect ! :) Thanks a lot
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'

1 Comment

For performance this is perhaps a better solution as it adds no additional overhead to the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.