jonn
jonn

Reputation: 1

Capture C function parameter name

I need to get the name of the function that is passed into a C function. I am hoping that some c macro that will allow at compile time, to get the ascii version of the C function name.

void foo( void )
{
    // nothing
}

typedef void (*pFunction)( void );

void ScheduleCallback( pFunction fName, int x, int y )
{

    // Is it possible to get the name "foo" in this routine?

} 

main()
{

    ScheduleCallback( foo, 3, 4 );

}

Is there a macro for a parameter in c?

I have tried using FUNCTION and then do a macro for the calling procedure like

ScheduleCallback( foo, 3, 4, __FUCNTION__ ).

With this I would get "main".

Upvotes: 0

Views: 64

Answers (1)

Alcamtar
Alcamtar

Reputation: 1582

As someone else suggested, it is easiest just to hardcode the name and pass it in. To avoid having to type the name twice I usually use a macro for this sort of thing, like this:

#define FUNC_WITH_NAME(x) x,#x

typedef void (*pFunction)();

void foo() {
}

void ScheduleCallback(pFunction func, char *name, int x, int y) {
    printf("scheduling %s\n", name);
}


int main() {
    ScheduleCallback(FUNC_WITH_NAME(foo),3,4);
}

Upvotes: 3

Related Questions