Non sono sicuro che questo è ciò che desideri, ma potresti fare qualcosa del genere. Dichiarare una struttura per contenere un nome di funzione e indirizzo, e una serie di funzioni nell'ambito di file:
#define FNUM 3
struct fnc {
void *addr;
char name[32];
};
void (*f[FNUM])();
struct fnc fnames[FNUM];
inizializzare questi nel codice manualmente nome della funzione, ad esempio
fnames[0] = (struct fnc){foo1, "foo1"}; // function address + its name
fnames[1] = (struct fnc){foo2, "foo2"};
fnames[2] = (struct fnc){foo3, "foo3"};
Creare una funzione per cercare l'array, ad es.
char *getfname(void *p)
{
for (int i = 0; i < FNUM; i++) {
if (fnames[i].addr == p)
return fnames[i].name;
}
return NULL;
}
Ho eseguito una rapida prova di questo. Ho inizializzato l'array in main
e chiamato foo1()
. Ecco la mia funzione e l'uscita:
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'
Oppure, più in generale:
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'
versione C++ @ http://stackoverflow.com/q/679021/139010 –
Non c'è modo standard di fare questo . Quale compilatore stai usando? –
Come ottenere il nome della funzione dal puntatore della funzione in C http://stackoverflow.com/questions/351134/how-to-get-functions-name-from-functions-pointer-in-c – zzk