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 :)
There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.
In summary, to call a function from a string, the functions getattr() , locals() , and globals() are used. getattr() will require you to know what object or module the function is located in, while locals() and globals() will locate the function within its own scope.
Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.
Method 1: Get Function Name in Python using function. func_name. By using a simple function property function, func_name, one can get the name of the function and hence can be quite handy for the Testing purpose and also for documentation at times.
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, ...); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With